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)