Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Install dependencies
|
2 |
+
!pip install openai gradio
|
3 |
+
|
4 |
+
import openai
|
5 |
+
import gradio as gr
|
6 |
+
|
7 |
+
# Set your OpenAI API key
|
8 |
+
openai.api_key = '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'
|
9 |
+
|
10 |
+
# Function to process the mathematical question
|
11 |
+
def math_chatbot(question):
|
12 |
+
try:
|
13 |
+
# Call the OpenAI API to get the answer
|
14 |
+
response = openai.ChatCompletion.create(
|
15 |
+
model="gpt-3.5-turbo", # You can also use "gpt-4" if you have access
|
16 |
+
messages=[
|
17 |
+
{"role": "user", "content": question}
|
18 |
+
]
|
19 |
+
)
|
20 |
+
# Extract the response text
|
21 |
+
answer = response['choices'][0]['message']['content']
|
22 |
+
except Exception as e:
|
23 |
+
answer = str(e)
|
24 |
+
|
25 |
+
return answer
|
26 |
+
|
27 |
+
# Gradio interface
|
28 |
+
iface = gr.Interface(
|
29 |
+
fn=math_chatbot,
|
30 |
+
inputs=gr.inputs.Textbox(label="Ask a Mathematical Question", placeholder="e.g., What is 2 + 2?"),
|
31 |
+
outputs="text",
|
32 |
+
title="Math Chatbot",
|
33 |
+
description="Ask any mathematical question and get an answer!"
|
34 |
+
)
|
35 |
+
|
36 |
+
# Launch the Gradio app
|
37 |
+
iface.launch()
|