File size: 1,231 Bytes
ff8891c
8b831d6
dda8103
8b831d6
 
0638681
 
 
dda8103
0638681
8b831d6
0638681
 
 
 
ec81c29
0638681
 
acd8a82
8b831d6
 
 
0638681
 
4cb5887
 
0638681
 
 
 
af0dd60
4cb5887
0638681
 
 
 
 
 
4cb5887
0638681
8b831d6
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
37
38
39
40
41
42
import gradio as gr
import openai

# Set your OpenAI API key
openai.api_key = "YOUR_API_KEY"  # Replace with your actual API key

# Maximum number of messages to keep in history
MAX_HISTORY = 10

def chat_fn(message, history):
    # Convert history to the correct format for OpenAI
    messages = []
    for user_msg, bot_msg in history[-MAX_HISTORY:]:  # Keep only the last MAX_HISTORY messages
        messages.append({"role": "user", "content": user_msg})
        messages.append({"role": "assistant", "content": bot_msg})

    # Add the new user message
    messages.append({"role": "user", "content": message})

    # Call OpenAI API
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=messages,
        max_tokens=300
    )

    # Get the model's reply
    bot_reply = response.choices[0].message["content"]

    # Update history
    history.append((message, bot_reply))

    # Return empty textbox and updated history
    return "", history

# Gradio interface
with gr.Blocks() as demo:
    gr.Markdown("# 💬 JuriX Chatbot")
    chatbot = gr.Chatbot()
    msg = gr.Textbox(label="Type your message here", placeholder="Enter your message...")
    msg.submit(chat_fn, [msg, chat]()_