app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748#genai.configure(api_key="AIzaSyB2FrTywdRUmHXICxRPYTN_TznajASTKDY")import streamlit as stimport google.generativeai as genai# Configure the API key for Gemini AIgenai.configure(api_key="AIzaSyD8tAz466UNCkLl83WBT05imim-JYL5Gr4")# AIzaSyB2FrTywdRUmHXICxRPYTN_TznajASTKDYmodel = genai.GenerativeModel("gemini-1.5-flash")# Streamlit App layoutst.title("AI-Powered Python Code Assistant")st.markdown("This assistant can help you with code suggestions, debugging, and reusable snippets using Gemini AI.")# Sidebarst.sidebar.header("Select Feature")feature = st.sidebar.selectbox("Choose what you need help with:",["Code Suggestions", "Code Debugging", "Reusable Code Snippets", "General Code Query"])# Input box for user to type code or queryuser_input = st.text_area("Enter your Python code or query here:")# Button to trigger the AI responseif st.button("Get AI Suggestions"):# Generate response based on selected featureif feature == "Code Suggestions":prompt = f"Suggest code completion for the following Python code:\n{user_input}"elif feature == "Code Debugging":prompt = f"Find and fix the bugs in the following Python code:\n{user_input}"elif feature == "Reusable Code Snippets":prompt = f"Provide reusable Python code snippets related to: {user_input}"else:prompt = f"Provide an explanation or advice on this Python code/query:\n{user_input}"# Get response from Gemini AI modelresponse = model.generate_content(prompt)# Display AI's response in the appst.subheader("AI's Response:")st.code(response.text, language="python")# Optionally, you can add a footer or additional features herest.markdown("---")st.markdown("Powered by **Streamlit** and **Google Gemini AI**")123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566# app.pyimport gradio as grSUBJECTS = ["Mathematics", "English", "Kiswahili", "Physics", "Chemistry", "Biology", "Islamic Studies"]WELCOME_MD = """# Elimuhub Tutor 🤝**Elimuhub Education Consultants** — Quick demo tutor (mobile friendly).Contact: +254 731-838-387• elimuhubconsultant@gmail.comType your question below or try quick commands:- Say "generate quiz: subject, level, n" e.g., `generate quiz: math, KCSE, 3`- Ask: "How do I solve quadratic equations?""""def generate_quiz(subject="General", level="Any", n=3):qlist = []for i in range(1, n+1):qlist.append(f"{i}. Sample {level} {subject} question #{i}?")return "\n".join(qlist)def respond(user_message, history):if history is None:history = []msg = (user_message or "").strip()if not msg:return history, ""low = msg.lower()if any(greet in low for greet in ["hi", "hello", "hey", "salaam", "assalamu"]):bot = "Hello! 👋 I'm Elimuhub's demo tutor. Tell me the subject and topic (e.g., 'math algebra')."elif low.startswith("generate quiz:"):try:rest = low.split("generate quiz:",1)[1].strip()parts = [p.strip() for p in rest.split(",")]subj = parts[0].title() if len(parts) > 0 and parts[0] else "General"level = parts[1].upper() if len(parts) > 1 and parts[1] else "Any"num = int(parts[2]) if len(parts) > 2 and parts[2].isdigit() else 3except Exception:subj, level, num = "General", "Any", 3bot = generate_quiz(subj, level, num)elif any(s.lower() in low for s in SUBJECTS):bot = ("Great — you asked about a subject I recognize. ""Please give the specific topic (e.g., algebra, verb tenses), and I'll give step-by-step help.")elif "kcse" in low or "kcpe" in low or "igcse" in low or "ib" in low:bot = "I can give exam-style tips and sample questions. Tell me the subject and the topic."else:# simple demo answer: echo plus tipsbot = ("Thanks — here's a quick answer guide:\n\n"f"> You asked: {user_message}\n\n""I'm a demo assistant. For clearer help, say the subject and topic (e.g., 'math: quadratic formula').")history.append((user_message, bot))return history, ""with gr.Blocks() as demo:gr.Markdown(WELCOME_MD)chatbot = gr.Chatbot(elem_id="chatbot", label="Elimuhub Tutor")with gr.Row():txt = gr.Textbox(show_label=False, placeholder="Type your question here and press Enter...")send = gr.Button("Send")send.click(respond, inputs=[txt, chatbot], outputs=[chatbot, txt])txt.submit(respond, inputs=[txt, chatbot], outputs=[chatbot, txt])if __name__ == "__main__":demo.launch()
app.py+58-40