File size: 4,782 Bytes
712eefd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import gradio as gr
from huggingface_hub import InferenceClient
import os  # For environment variables

# Initialize the Hugging Face Inference Client
client = InferenceClient()

# Functions for solving problems, generating hints, and more
def solve_math_problem(problem, difficulty):
    prompt = os.getenv("PROMPT_SOLVE").format(problem=problem, difficulty=difficulty)
    response = client.chat.completions.create(
        model="Qwen/QwQ-32B-Preview",
        messages=[{"role": "user", "content": prompt}],
        temperature=0.7,
        max_tokens=1024,
        top_p=0.8
    )
    return response.choices[0].message["content"]

def generate_hint(problem, difficulty):
    prompt = os.getenv("PROMPT_HINT").format(problem=problem, difficulty=difficulty)
    response = client.chat.completions.create(
        model="Qwen/QwQ-32B-Preview",
        messages=[{"role": "user", "content": prompt}],
        temperature=0.7,
        max_tokens=512,
        top_p=0.8
    )
    return response.choices[0].message["content"]

def verify_solution(problem, solution):
    prompt = os.getenv("PROMPT_VERIFY").format(problem=problem, solution=solution)
    response = client.chat.completions.create(
        model="Qwen/QwQ-32B-Preview",
        messages=[{"role": "user", "content": prompt}],
        temperature=0.7,
        max_tokens=512,
        top_p=0.8
    )
    return response.choices[0].message["content"]

def generate_practice_question(topic, difficulty):
    prompt = os.getenv("PROMPT_GENERATE").format(topic=topic, difficulty=difficulty)
    response = client.chat.completions.create(
        model="Qwen/QwQ-32B-Preview",
        messages=[{"role": "user", "content": prompt}],
        temperature=0.7,
        max_tokens=512,
        top_p=0.8
    )
    return response.choices[0].message["content"]

def explain_concept(problem, difficulty):
    prompt = os.getenv("PROMPT_EXPLAIN").format(problem=problem, difficulty=difficulty)
    response = client.chat.completions.create(
        model="Qwen/QwQ-32B-Preview",
        messages=[{"role": "user", "content": prompt}],
        temperature=0.7,
        max_tokens=512,
        top_p=0.8
    )
    return response.choices[0].message["content"]

# Create Gradio interface
with gr.Blocks() as app:
    gr.Markdown("## Mathematical Insight Tutor")
    gr.Markdown("An advanced AI-powered tutor to help you master math concepts.")

    with gr.Tab("Solve a Problem"):
        problem_input = gr.Textbox(label="Enter Math Problem", placeholder="e.g., Solve for x: 2x + 5 = 15")
        difficulty_level = gr.Dropdown(choices=["Beginner", "Intermediate", "Advanced"], label="Difficulty Level")
        solve_button = gr.Button("Solve")
        solution_output = gr.Markdown()
        solve_button.click(solve_math_problem, inputs=[problem_input, difficulty_level], outputs=solution_output)

    with gr.Tab("Generate a Hint"):
        hint_problem_input = gr.Textbox(label="Enter Math Problem for Hint", placeholder="e.g., Solve for x: 2x + 5 = 15")
        hint_difficulty = gr.Dropdown(choices=["Beginner", "Intermediate", "Advanced"], label="Difficulty Level")
        hint_button = gr.Button("Generate Hint")
        hint_output = gr.Markdown()
        hint_button.click(generate_hint, inputs=[hint_problem_input, hint_difficulty], outputs=hint_output)

    with gr.Tab("Verify Solution"):
        verify_problem_input = gr.Textbox(label="Enter Math Problem", placeholder="e.g., Solve for x: 2x + 5 = 15")
        solution_input = gr.Textbox(label="Enter Your Solution", placeholder="e.g., x = 5")
        verify_button = gr.Button("Verify Solution")
        verify_output = gr.Markdown()
        verify_button.click(verify_solution, inputs=[verify_problem_input, solution_input], outputs=verify_output)

    with gr.Tab("Generate Practice Question"):
        topic_input = gr.Textbox(label="Enter Math Topic", placeholder="e.g., Algebra, Calculus")
        practice_difficulty = gr.Dropdown(choices=["Beginner", "Intermediate", "Advanced"], label="Difficulty Level")
        generate_button = gr.Button("Generate Question")
        practice_output = gr.Markdown()
        generate_button.click(generate_practice_question, inputs=[topic_input, practice_difficulty], outputs=practice_output)

    with gr.Tab("Explain Concept"):
        explain_problem_input = gr.Textbox(label="Enter Math Problem", placeholder="e.g., Solve for x: 2x + 5 = 15")
        explain_difficulty = gr.Dropdown(choices=["Beginner", "Intermediate", "Advanced"], label="Difficulty Level")
        explain_button = gr.Button("Explain Concept")
        explain_output = gr.Markdown()
        explain_button.click(explain_concept, inputs=[explain_problem_input, explain_difficulty], outputs=explain_output)

# Launch the app
app.launch(debug=True)