Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import openai
|
| 2 |
+
import gradio
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
openai.api_key = os.environ.get("API_TOKEN")
|
| 6 |
+
|
| 7 |
+
messages = [{"role": "system", "content": "You are an education expert who can explain everything, even complicated subjects, so well that a child can understand"}]
|
| 8 |
+
|
| 9 |
+
def CheckAssist(user_input):
|
| 10 |
+
messages.append({"role": "user", "content": user_input})
|
| 11 |
+
|
| 12 |
+
# Save the user question to a file
|
| 13 |
+
with open("user_questions.txt", "a") as f:
|
| 14 |
+
f.write(user_input + "\n")
|
| 15 |
+
|
| 16 |
+
response = openai.ChatCompletion.create(
|
| 17 |
+
model="gpt-3.5-turbo",
|
| 18 |
+
messages=messages[-5:], # Change this line
|
| 19 |
+
)
|
| 20 |
+
ChatGPT_reply = response["choices"][0]["message"]["content"]
|
| 21 |
+
messages.append({"role": "assistant", "content": ChatGPT_reply})
|
| 22 |
+
|
| 23 |
+
# Keep only the last five messages in the list
|
| 24 |
+
if len(messages) > 10: # Change this line
|
| 25 |
+
messages.pop(0)
|
| 26 |
+
messages.pop(0)
|
| 27 |
+
|
| 28 |
+
return ChatGPT_reply
|
| 29 |
+
|
| 30 |
+
demo = gradio.Interface(
|
| 31 |
+
fn=CheckAssist,
|
| 32 |
+
inputs=gradio.inputs.Textbox(label="Your Question"),
|
| 33 |
+
outputs=gradio.outputs.Textbox(label="Assistant's Answer"),
|
| 34 |
+
title="Teacher Jihan's Teaching Assistant"
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
demo.launch(share=False, debug=True)
|