shukdevdatta123 commited on
Commit
97bfb7b
·
verified ·
1 Parent(s): 85415db

Create v1.txt

Browse files
Files changed (1) hide show
  1. v1.txt +183 -0
v1.txt ADDED
@@ -0,0 +1,183 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import os
3
+ import re
4
+ from openai import OpenAI
5
+ import time
6
+
7
+ # Function to generate math solution using the Phi-4-reasoning-plus model
8
+ def generate_math_solution(api_key, problem_text, history=None):
9
+ if not api_key.strip():
10
+ return "Please enter your OpenRouter API key.", history
11
+
12
+ if not problem_text.strip():
13
+ return "Please enter a math problem.", history
14
+
15
+ try:
16
+ client = OpenAI(
17
+ base_url="https://openrouter.ai/api/v1",
18
+ api_key=api_key,
19
+ )
20
+
21
+ messages = [
22
+ {"role": "system", "content":
23
+ """You are an expert math tutor who explains concepts clearly and thoroughly.
24
+ Analyze the given math problem and provide a detailed step-by-step solution.
25
+ For each step:
26
+ 1. Show the mathematical operation
27
+ 2. Explain why this step is necessary
28
+ 3. Connect it to relevant mathematical concepts
29
+
30
+ Format your response with clear section headers using markdown.
31
+ Begin with an "Initial Analysis" section, follow with numbered steps,
32
+ and conclude with a "Final Answer" section."""},
33
+ ]
34
+
35
+ # Add conversation history if it exists
36
+ if history:
37
+ for exchange in history:
38
+ messages.append({"role": "user", "content": exchange[0]})
39
+ if exchange[1]: # Check if there's a response
40
+ messages.append({"role": "assistant", "content": exchange[1]})
41
+
42
+ # Add the current problem
43
+ messages.append({"role": "user", "content": f"Solve this math problem step-by-step: {problem_text}"})
44
+
45
+ # Create the completion
46
+ completion = client.chat.completions.create(
47
+ model="microsoft/phi-4-reasoning-plus:free",
48
+ messages=messages,
49
+ extra_headers={
50
+ "HTTP-Referer": "https://advancedmathtutor.edu",
51
+ "X-Title": "Advanced Math Tutor",
52
+ }
53
+ )
54
+
55
+ solution = completion.choices[0].message.content
56
+
57
+ # Update history
58
+ if history is None:
59
+ history = []
60
+ history.append((problem_text, solution))
61
+
62
+ return solution, history
63
+
64
+ except Exception as e:
65
+ error_message = f"Error: {str(e)}"
66
+ return error_message, history
67
+
68
+ # Function to verify API key format
69
+ def validate_api_key(api_key):
70
+ # This is a simple check - OpenRouter keys typically start with "sk-or-"
71
+ if api_key.startswith("sk-or-") and len(api_key) > 20:
72
+ return True
73
+ return False
74
+
75
+ # Function to process LaTeX in the solution
76
+ def process_solution(solution):
77
+ # Replace $...$ with $$...$$ for better rendering in Gradio markdown
78
+ solution = re.sub(r'(?<!\$)\$(?!\$)(.+?)(?<!\$)\$(?!\$)', r'$$\1$$', solution)
79
+ return solution
80
+
81
+ # Define the Gradio interface
82
+ def create_demo():
83
+ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
84
+ gr.Markdown("# 📚 Advanced Math Tutor")
85
+ gr.Markdown("""
86
+ This application uses Microsoft's Phi-4-reasoning-plus model to provide step-by-step solutions
87
+ to math problems. Enter your math problem, and get a detailed explanation with clear reasoning steps.
88
+ """)
89
+
90
+ # Main tabs
91
+ with gr.Tabs():
92
+ with gr.TabItem("Problem Solver"):
93
+ with gr.Row():
94
+ with gr.Column(scale=1):
95
+ api_key_input = gr.Textbox(
96
+ label="OpenRouter API Key",
97
+ placeholder="Enter your OpenRouter API key (starts with sk-or-)",
98
+ type="password"
99
+ )
100
+ problem_input = gr.Textbox(
101
+ label="Math Problem",
102
+ placeholder="Enter your math problem here...",
103
+ lines=5
104
+ )
105
+ example_problems = gr.Examples(
106
+ examples=[
107
+ ["Solve the quadratic equation: 3x² + 5x - 2 = 0"],
108
+ ["Find the derivative of f(x) = x³ln(x)"],
109
+ ["Calculate the area of a circle with radius 5 cm"],
110
+ ["Find all values of x that satisfy the equation: log₂(x-1) + log₂(x+3) = 5"]
111
+ ],
112
+ inputs=[problem_input],
113
+ label="Example Problems"
114
+ )
115
+ with gr.Row():
116
+ submit_btn = gr.Button("Solve Problem", variant="primary")
117
+ clear_btn = gr.Button("Clear")
118
+
119
+ with gr.Column(scale=2):
120
+ solution_output = gr.Markdown(label="Solution")
121
+
122
+ # Store conversation history (invisible to user)
123
+ conversation_history = gr.State(value=None)
124
+
125
+ # Button actions
126
+ submit_btn.click(
127
+ fn=generate_math_solution,
128
+ inputs=[api_key_input, problem_input, conversation_history],
129
+ outputs=[solution_output, conversation_history]
130
+ )
131
+
132
+ clear_btn.click(
133
+ fn=lambda: ("", None),
134
+ inputs=[],
135
+ outputs=[solution_output, conversation_history]
136
+ )
137
+
138
+ with gr.TabItem("Help"):
139
+ gr.Markdown("""
140
+ ## How to Use the Advanced Math Tutor
141
+
142
+ ### Getting Started
143
+ 1. You'll need an API key from OpenRouter to use this app
144
+ 2. Sign up at [OpenRouter](https://openrouter.ai/) to get your API key
145
+ 3. Enter your API key in the designated field
146
+
147
+ ### Solving Math Problems
148
+ - Type or paste your math problem in the input field
149
+ - Click "Solve Problem" to get a detailed step-by-step solution
150
+ - The solution will include explanations for each step
151
+ - You can also try one of the provided example problems
152
+
153
+ ### Tips for Best Results
154
+ - Be specific in your problem description
155
+ - Include all necessary information
156
+ - For complex equations, use clear notation
157
+ - For algebraic expressions, use ^ for exponents (e.g., x^2 for x²)
158
+ - Use parentheses to group terms clearly
159
+
160
+ ### Types of Problems You Can Solve
161
+ - Algebra (equations, inequalities, systems of equations)
162
+ - Calculus (derivatives, integrals, limits)
163
+ - Trigonometry
164
+ - Geometry
165
+ - Statistics and Probability
166
+ - Number Theory
167
+ - And many more!
168
+ """)
169
+
170
+ # Footer
171
+ gr.Markdown("""
172
+ ---
173
+ ### About
174
+ This application uses Microsoft's Phi-4-reasoning-plus model via OpenRouter to generate step-by-step solutions.
175
+ Your API key is required but not stored permanently.
176
+ """)
177
+
178
+ return demo
179
+
180
+ # Launch the app
181
+ if __name__ == "__main__":
182
+ demo = create_demo()
183
+ demo.launch()