Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,34 +1,47 @@
|
|
1 |
-
import openai
|
2 |
import gradio as gr
|
|
|
|
|
3 |
|
4 |
-
#
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
# Function to process the mathematical question
|
8 |
-
def math_chatbot(question):
|
9 |
try:
|
10 |
-
|
11 |
-
|
12 |
-
model="gpt-3.5-turbo", # You can also use "gpt-4" if you have access
|
13 |
-
messages=[
|
14 |
-
{"role": "user", "content": question}
|
15 |
-
]
|
16 |
-
)
|
17 |
-
# Extract the response text
|
18 |
-
answer = response['choices'][0]['message']['content']
|
19 |
except Exception as e:
|
20 |
-
|
21 |
|
22 |
-
|
|
|
23 |
|
24 |
-
# Gradio
|
25 |
-
|
26 |
-
|
27 |
-
inputs=gr.Textbox(label="Ask a Mathematical Question", placeholder="e.g., What is 2 + 2?"),
|
28 |
-
outputs="text",
|
29 |
-
title="Math Chatbot",
|
30 |
-
description="Ask any mathematical question and get an answer!"
|
31 |
-
)
|
32 |
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoTokenizer, pipeline, GPTNeoForCausalLM
|
3 |
+
import sympy as sp
|
4 |
|
5 |
+
# Load model function
|
6 |
+
def load_model():
|
7 |
+
try:
|
8 |
+
model = GPTNeoForCausalLM.from_pretrained("EleutherAI/gpt-neo-125M")
|
9 |
+
tokenizer = AutoTokenizer.from_pretrained("EleutherAI/gpt-neo-125M")
|
10 |
+
generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
11 |
+
return generator
|
12 |
+
except Exception as e:
|
13 |
+
return f"Error loading model: {e}"
|
14 |
+
|
15 |
+
# Function to check if the question is mathematical
|
16 |
+
def is_mathematical_question(question):
|
17 |
+
keywords = ["mean", "area", "volume", "solve", "what is", "calculate", "add", "subtract", "multiply", "divide"]
|
18 |
+
return any(keyword in question.lower() for keyword in keywords)
|
19 |
+
|
20 |
+
# Function to process and evaluate the question
|
21 |
+
def process_math_question(question):
|
22 |
+
if not is_mathematical_question(question):
|
23 |
+
return "This chatbot only answers questions related to mathematics. Please ask a mathematical question."
|
24 |
|
|
|
|
|
25 |
try:
|
26 |
+
result = sp.sympify(question)
|
27 |
+
return f"The answer is: {result}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
except Exception as e:
|
29 |
+
return f"Could not evaluate the expression. Error: {str(e)}"
|
30 |
|
31 |
+
# Load the model once when starting the app
|
32 |
+
generator = load_model()
|
33 |
|
34 |
+
# Gradio Interface
|
35 |
+
def chatbot_interface(user_input):
|
36 |
+
return process_math_question(user_input)
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
+
if __name__ == "__main__":
|
39 |
+
# Create Gradio Interface
|
40 |
+
iface = gr.Interface(
|
41 |
+
fn=chatbot_interface,
|
42 |
+
inputs=gr.inputs.Textbox(label="Enter your mathematical question:"),
|
43 |
+
outputs="text",
|
44 |
+
title="Math Chatbot (Open Source)",
|
45 |
+
description="Ask any mathematical question and get an answer. Non-mathematical questions will be restricted."
|
46 |
+
)
|
47 |
+
iface.launch()
|