app.py
#1
by elimuhub - opened
11233445676789109101112131415121314161718192021161718192021222324252627282923243031323334353637383940414243444546474849505152262753545529303132333435363738394041424344454647485657585960616263646566#genai.configure(api_key="AIzaSyB2FrTywdRUmHXICxRPYTN_TznajASTKDY")# app.pyimport gradio as gr
import streamlit as stimport google.generativeai as genaiSUBJECTS = ["Mathematics", "English", "Kiswahili", "Physics", "Chemistry", "Biology", "Islamic Studies"]
# Configure the API key for Gemini AIgenai.configure(api_key="AIzaSyD8tAz466UNCkLl83WBT05imim-JYL5Gr4")WELCOME_MD = """# Elimuhub Tutor 🤝**Elimuhub Education Consultants** — Quick demo tutor (mobile friendly).Contact: +254 731-838-387• elimuhubconsultant@gmail.com
# AIzaSyB2FrTywdRUmHXICxRPYTN_TznajASTKDYmodel = genai.GenerativeModel("gemini-1.5-flash")Type 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?""""
# 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.")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)
# 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"])def respond(user_message, history): if history is None: history = [] msg = (user_message or "").strip() if not msg: return history, "" low = msg.lower()
# Input box for user to type code or queryuser_input = st.text_area("Enter your Python code or query here:") 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 3 except Exception: subj, level, num = "General", "Any", 3 bot = 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 tips bot = ("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').")
# Button to trigger the AI responseif st.button("Get AI Suggestions"): history.append((user_message, bot)) return history, ""
# Generate response based on selected feature if 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 model response = model.generate_content(prompt) # Display AI's response in the app st.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**")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