Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,38 +1,13 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
import os
|
4 |
-
|
5 |
|
6 |
# Initialize the Hugging Face Inference Client
|
7 |
client = InferenceClient()
|
8 |
|
9 |
|
10 |
|
11 |
-
def preprocess_latex(content):
|
12 |
-
"""
|
13 |
-
Preprocess the AI response using sympy to properly format mathematical expressions.
|
14 |
-
"""
|
15 |
-
lines = content.split("\n") # Split response into lines
|
16 |
-
processed_lines = []
|
17 |
-
|
18 |
-
for line in lines:
|
19 |
-
if "[" in line and "]" in line: # Detect mathematical expressions in brackets
|
20 |
-
while "[" in line and "]" in line:
|
21 |
-
start = line.index("[")
|
22 |
-
end = line.index("]") + 1
|
23 |
-
math_expr = line[start + 1 : end - 1].strip()
|
24 |
-
try:
|
25 |
-
# Parse the math expression and convert to LaTeX
|
26 |
-
sympy_expr = sympify(math_expr)
|
27 |
-
latex_expr = latex(sympy_expr)
|
28 |
-
line = line[:start] + f"$$ {latex_expr} $$" + line[end:]
|
29 |
-
except Exception:
|
30 |
-
# Fallback to raw math if parsing fails
|
31 |
-
line = line[:start] + f"$$ {math_expr} $$" + line[end:]
|
32 |
-
processed_lines.append(line)
|
33 |
-
|
34 |
-
return "\n".join(processed_lines)
|
35 |
-
|
36 |
|
37 |
|
38 |
# Function to generate and format AI response
|
@@ -47,9 +22,7 @@ def generate_response(prompt_template, **kwargs):
|
|
47 |
top_p=0.8
|
48 |
)
|
49 |
response_content = response.choices[0].message["content"]
|
50 |
-
|
51 |
-
formatted_response = preprocess_latex(response_content)
|
52 |
-
return gr.update(value=f"{formatted_response}")
|
53 |
|
54 |
# Gradio app interface
|
55 |
with gr.Blocks() as app:
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
import os
|
4 |
+
|
5 |
|
6 |
# Initialize the Hugging Face Inference Client
|
7 |
client = InferenceClient()
|
8 |
|
9 |
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
|
13 |
# Function to generate and format AI response
|
|
|
22 |
top_p=0.8
|
23 |
)
|
24 |
response_content = response.choices[0].message["content"]
|
25 |
+
return gr.update(value=f"{response_content}")
|
|
|
|
|
26 |
|
27 |
# Gradio app interface
|
28 |
with gr.Blocks() as app:
|