File size: 1,022 Bytes
3ea0348
af17f97
 
 
 
 
 
 
 
3ea0348
af17f97
 
 
 
 
 
 
 
3ea0348
af17f97
3ea0348
 
 
 
af17f97
 
3ea0348
af17f97
3ea0348
 
 
 
af17f97
3ea0348
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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()