Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import os
|
3 |
+
import shelve
|
4 |
+
from g4f.client import Client
|
5 |
+
|
6 |
+
USER_AVATAR = "👤"
|
7 |
+
BOT_AVATAR = "🤖"
|
8 |
+
client = Client()
|
9 |
+
|
10 |
+
# Initialize chat history
|
11 |
+
def load_chat_history():
|
12 |
+
with shelve.open("chat_history") as db:
|
13 |
+
return db.get("messages", [])
|
14 |
+
|
15 |
+
def save_chat_history(messages):
|
16 |
+
with shelve.open("chat_history") as db:
|
17 |
+
db["messages"] = messages
|
18 |
+
|
19 |
+
chat_history = load_chat_history()
|
20 |
+
|
21 |
+
def chatbot_interface(user_input):
|
22 |
+
global chat_history
|
23 |
+
if user_input:
|
24 |
+
chat_history.append({"role": "user", "content": user_input})
|
25 |
+
response = client.chat.completions.create(
|
26 |
+
model="gpt-3.5-turbo",
|
27 |
+
messages=chat_history,
|
28 |
+
)
|
29 |
+
bot_response = response.choices[0].message.content
|
30 |
+
chat_history.append({"role": "assistant", "content": bot_response})
|
31 |
+
save_chat_history(chat_history)
|
32 |
+
return bot_response
|
33 |
+
|
34 |
+
iface = gr.Interface(fn=chatbot_interface, inputs="text", outputs="text", title="Gradio Chatbot Interface")
|
35 |
+
iface.launch()
|