Update app.py
Browse files
app.py
CHANGED
|
@@ -1,14 +1,14 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
|
| 7 |
# Maximum number of messages to keep in history
|
| 8 |
MAX_HISTORY = 10
|
| 9 |
|
| 10 |
def chat_fn(message, history):
|
| 11 |
-
# Convert history to the correct format for
|
| 12 |
messages = []
|
| 13 |
for user_msg, bot_msg in history[-MAX_HISTORY:]: # Keep only the last MAX_HISTORY messages
|
| 14 |
messages.append({"role": "user", "content": user_msg})
|
|
@@ -17,8 +17,9 @@ def chat_fn(message, history):
|
|
| 17 |
# Add the new user message
|
| 18 |
messages.append({"role": "user", "content": message})
|
| 19 |
|
| 20 |
-
# Call
|
| 21 |
-
response =
|
|
|
|
| 22 |
messages=messages,
|
| 23 |
max_tokens=300
|
| 24 |
)
|
|
@@ -37,6 +38,4 @@ with gr.Blocks() as demo:
|
|
| 37 |
gr.Markdown("# 💬 JuriX Chatbot")
|
| 38 |
chatbot = gr.Chatbot()
|
| 39 |
msg = gr.Textbox(label="Type your message here", placeholder="Enter your message...")
|
| 40 |
-
msg.submit(chat_fn, [msg,
|
| 41 |
-
|
| 42 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import openai
|
| 3 |
|
| 4 |
+
# Set your OpenAI API key
|
| 5 |
+
openai.api_key = "YOUR_API_KEY" # Replace with your actual API key
|
| 6 |
|
| 7 |
# Maximum number of messages to keep in history
|
| 8 |
MAX_HISTORY = 10
|
| 9 |
|
| 10 |
def chat_fn(message, history):
|
| 11 |
+
# Convert history to the correct format for OpenAI
|
| 12 |
messages = []
|
| 13 |
for user_msg, bot_msg in history[-MAX_HISTORY:]: # Keep only the last MAX_HISTORY messages
|
| 14 |
messages.append({"role": "user", "content": user_msg})
|
|
|
|
| 17 |
# Add the new user message
|
| 18 |
messages.append({"role": "user", "content": message})
|
| 19 |
|
| 20 |
+
# Call OpenAI API
|
| 21 |
+
response = openai.ChatCompletion.create(
|
| 22 |
+
model="gpt-3.5-turbo",
|
| 23 |
messages=messages,
|
| 24 |
max_tokens=300
|
| 25 |
)
|
|
|
|
| 38 |
gr.Markdown("# 💬 JuriX Chatbot")
|
| 39 |
chatbot = gr.Chatbot()
|
| 40 |
msg = gr.Textbox(label="Type your message here", placeholder="Enter your message...")
|
| 41 |
+
msg.submit(chat_fn, [msg, chat]()_
|
|
|
|
|
|