Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import google.generativeai as genai
|
3 |
+
from functools import lru_cache
|
4 |
+
import time
|
5 |
+
|
6 |
+
# Initialize Gemini API (replace with your actual API key)
|
7 |
+
genai.configure(api_key="AIzaSyBPQF0g5EfEPzEiGRzA3iNzJZK4jDukMvE")
|
8 |
+
|
9 |
+
# Initialize the model
|
10 |
+
model = genai.GenerativeModel('gemini-pro')
|
11 |
+
|
12 |
+
# Cache for exercises
|
13 |
+
@lru_cache(maxsize=100)
|
14 |
+
def get_coding_exercise(topic, difficulty):
|
15 |
+
"""Generate a coding exercise based on the given topic and difficulty."""
|
16 |
+
prompt = f"Briefly create a {difficulty} Python coding exercise about {topic}. Include a concise problem statement and expected output. Keep it under 100 words."
|
17 |
+
try:
|
18 |
+
response = model.generate_content(prompt, timeout=30)
|
19 |
+
return response.text
|
20 |
+
except Exception as e:
|
21 |
+
return f"Error generating exercise: {str(e)}"
|
22 |
+
|
23 |
+
def evaluate_code(exercise, user_code):
|
24 |
+
"""Evaluate the user's code submission."""
|
25 |
+
prompt = f"""
|
26 |
+
Exercise: {exercise}
|
27 |
+
|
28 |
+
User's code:
|
29 |
+
{user_code}
|
30 |
+
|
31 |
+
Briefly evaluate the code. Provide concise feedback on correctness, efficiency, and style.
|
32 |
+
If there are errors, explain them. Suggest improvements. Keep the response under 150 words.
|
33 |
+
"""
|
34 |
+
try:
|
35 |
+
response = model.generate_content(prompt, timeout=30)
|
36 |
+
return response.text
|
37 |
+
except Exception as e:
|
38 |
+
return f"Error evaluating code: {str(e)}"
|
39 |
+
|
40 |
+
def tutor_interface(topic, difficulty):
|
41 |
+
with gr.Row():
|
42 |
+
gr.Markdown("Generating exercise...")
|
43 |
+
time.sleep(0.1) # Small delay to ensure loading message is shown
|
44 |
+
exercise = get_coding_exercise(topic, difficulty)
|
45 |
+
return exercise
|
46 |
+
|
47 |
+
def submit_solution(exercise, user_code):
|
48 |
+
with gr.Row():
|
49 |
+
gr.Markdown("Evaluating solution...")
|
50 |
+
time.sleep(0.1) # Small delay to ensure loading message is shown
|
51 |
+
feedback = evaluate_code(exercise, user_code)
|
52 |
+
return feedback
|
53 |
+
|
54 |
+
# Create the Gradio interface
|
55 |
+
with gr.Blocks() as demo:
|
56 |
+
gr.Markdown("# Intelligent Code Tutor")
|
57 |
+
|
58 |
+
with gr.Row():
|
59 |
+
topic_input = gr.Textbox(label="Topic (e.g., 'loops', 'lists', 'functions')")
|
60 |
+
difficulty_input = gr.Dropdown(["easy", "medium", "hard"], label="Difficulty")
|
61 |
+
|
62 |
+
generate_btn = gr.Button("Generate Exercise")
|
63 |
+
exercise_output = gr.Textbox(label="Coding Exercise", lines=10)
|
64 |
+
|
65 |
+
generate_btn.click(tutor_interface, inputs=[topic_input, difficulty_input], outputs=exercise_output)
|
66 |
+
|
67 |
+
code_input = gr.Code(language="python", label="Your Solution")
|
68 |
+
submit_btn = gr.Button("Submit Solution")
|
69 |
+
feedback_output = gr.Textbox(label="Feedback", lines=10)
|
70 |
+
|
71 |
+
submit_btn.click(submit_solution, inputs=[exercise_output, code_input], outputs=feedback_output)
|
72 |
+
|
73 |
+
if __name__ == "__main__":
|
74 |
+
demo.launch()
|