Spaces:
Runtime error
Runtime error
import gradio as gr | |
import os | |
import shelve | |
from g4f.client import Client | |
USER_AVATAR = "π€" | |
BOT_AVATAR = "π€" | |
client = Client() | |
# Initialize chat history | |
def load_chat_history(): | |
with shelve.open("chat_history") as db: | |
return db.get("messages", []) | |
def save_chat_history(messages): | |
with shelve.open("chat_history") as db: | |
db["messages"] = messages | |
chat_history = load_chat_history() | |
def chatbot_interface(user_input): | |
global chat_history | |
if user_input: | |
chat_history.append({"role": "user", "content": user_input}) | |
response = client.chat.completions.create( | |
model="gpt-3.5-turbo", | |
messages=chat_history, | |
) | |
bot_response = response.choices[0].message.content | |
chat_history.append({"role": "assistant", "content": bot_response}) | |
save_chat_history(chat_history) | |
return bot_response | |
iface = gr.Interface(fn=chatbot_interface, inputs="text", outputs="text", title="Gradio Chatbot Interface") | |
iface.launch() | |