yasserrmd commited on
Commit
712eefd
·
verified ·
1 Parent(s): 9195d97

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -0
app.py ADDED
@@ -0,0 +1,105 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from huggingface_hub import InferenceClient
3
+ import os # For environment variables
4
+
5
+ # Initialize the Hugging Face Inference Client
6
+ client = InferenceClient()
7
+
8
+ # Functions for solving problems, generating hints, and more
9
+ def solve_math_problem(problem, difficulty):
10
+ prompt = os.getenv("PROMPT_SOLVE").format(problem=problem, difficulty=difficulty)
11
+ response = client.chat.completions.create(
12
+ model="Qwen/QwQ-32B-Preview",
13
+ messages=[{"role": "user", "content": prompt}],
14
+ temperature=0.7,
15
+ max_tokens=1024,
16
+ top_p=0.8
17
+ )
18
+ return response.choices[0].message["content"]
19
+
20
+ def generate_hint(problem, difficulty):
21
+ prompt = os.getenv("PROMPT_HINT").format(problem=problem, difficulty=difficulty)
22
+ response = client.chat.completions.create(
23
+ model="Qwen/QwQ-32B-Preview",
24
+ messages=[{"role": "user", "content": prompt}],
25
+ temperature=0.7,
26
+ max_tokens=512,
27
+ top_p=0.8
28
+ )
29
+ return response.choices[0].message["content"]
30
+
31
+ def verify_solution(problem, solution):
32
+ prompt = os.getenv("PROMPT_VERIFY").format(problem=problem, solution=solution)
33
+ response = client.chat.completions.create(
34
+ model="Qwen/QwQ-32B-Preview",
35
+ messages=[{"role": "user", "content": prompt}],
36
+ temperature=0.7,
37
+ max_tokens=512,
38
+ top_p=0.8
39
+ )
40
+ return response.choices[0].message["content"]
41
+
42
+ def generate_practice_question(topic, difficulty):
43
+ prompt = os.getenv("PROMPT_GENERATE").format(topic=topic, difficulty=difficulty)
44
+ response = client.chat.completions.create(
45
+ model="Qwen/QwQ-32B-Preview",
46
+ messages=[{"role": "user", "content": prompt}],
47
+ temperature=0.7,
48
+ max_tokens=512,
49
+ top_p=0.8
50
+ )
51
+ return response.choices[0].message["content"]
52
+
53
+ def explain_concept(problem, difficulty):
54
+ prompt = os.getenv("PROMPT_EXPLAIN").format(problem=problem, difficulty=difficulty)
55
+ response = client.chat.completions.create(
56
+ model="Qwen/QwQ-32B-Preview",
57
+ messages=[{"role": "user", "content": prompt}],
58
+ temperature=0.7,
59
+ max_tokens=512,
60
+ top_p=0.8
61
+ )
62
+ return response.choices[0].message["content"]
63
+
64
+ # Create Gradio interface
65
+ with gr.Blocks() as app:
66
+ gr.Markdown("## Mathematical Insight Tutor")
67
+ gr.Markdown("An advanced AI-powered tutor to help you master math concepts.")
68
+
69
+ with gr.Tab("Solve a Problem"):
70
+ problem_input = gr.Textbox(label="Enter Math Problem", placeholder="e.g., Solve for x: 2x + 5 = 15")
71
+ difficulty_level = gr.Dropdown(choices=["Beginner", "Intermediate", "Advanced"], label="Difficulty Level")
72
+ solve_button = gr.Button("Solve")
73
+ solution_output = gr.Markdown()
74
+ solve_button.click(solve_math_problem, inputs=[problem_input, difficulty_level], outputs=solution_output)
75
+
76
+ with gr.Tab("Generate a Hint"):
77
+ hint_problem_input = gr.Textbox(label="Enter Math Problem for Hint", placeholder="e.g., Solve for x: 2x + 5 = 15")
78
+ hint_difficulty = gr.Dropdown(choices=["Beginner", "Intermediate", "Advanced"], label="Difficulty Level")
79
+ hint_button = gr.Button("Generate Hint")
80
+ hint_output = gr.Markdown()
81
+ hint_button.click(generate_hint, inputs=[hint_problem_input, hint_difficulty], outputs=hint_output)
82
+
83
+ with gr.Tab("Verify Solution"):
84
+ verify_problem_input = gr.Textbox(label="Enter Math Problem", placeholder="e.g., Solve for x: 2x + 5 = 15")
85
+ solution_input = gr.Textbox(label="Enter Your Solution", placeholder="e.g., x = 5")
86
+ verify_button = gr.Button("Verify Solution")
87
+ verify_output = gr.Markdown()
88
+ verify_button.click(verify_solution, inputs=[verify_problem_input, solution_input], outputs=verify_output)
89
+
90
+ with gr.Tab("Generate Practice Question"):
91
+ topic_input = gr.Textbox(label="Enter Math Topic", placeholder="e.g., Algebra, Calculus")
92
+ practice_difficulty = gr.Dropdown(choices=["Beginner", "Intermediate", "Advanced"], label="Difficulty Level")
93
+ generate_button = gr.Button("Generate Question")
94
+ practice_output = gr.Markdown()
95
+ generate_button.click(generate_practice_question, inputs=[topic_input, practice_difficulty], outputs=practice_output)
96
+
97
+ with gr.Tab("Explain Concept"):
98
+ explain_problem_input = gr.Textbox(label="Enter Math Problem", placeholder="e.g., Solve for x: 2x + 5 = 15")
99
+ explain_difficulty = gr.Dropdown(choices=["Beginner", "Intermediate", "Advanced"], label="Difficulty Level")
100
+ explain_button = gr.Button("Explain Concept")
101
+ explain_output = gr.Markdown()
102
+ explain_button.click(explain_concept, inputs=[explain_problem_input, explain_difficulty], outputs=explain_output)
103
+
104
+ # Launch the app
105
+ app.launch(debug=True)