Spaces:
Build error
Build error
Commit
Β·
76e438b
1
Parent(s):
96bf1ae
chatbot running
Browse files- utils/chatbot.py +48 -46
utils/chatbot.py
CHANGED
|
@@ -1,53 +1,55 @@
|
|
| 1 |
-
from
|
| 2 |
import gradio as gr
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
client = OpenAI(
|
| 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 |
-
|
| 43 |
-
history = [history]
|
| 44 |
-
history.append({"role": "user", "content": message})
|
| 45 |
-
history.append({"role": "assistant", "content": doctor_response})
|
| 46 |
-
return [{"role": "assistant", "content": doctor_response}], history
|
| 47 |
-
|
| 48 |
|
| 49 |
# Create the Gradio interface
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
# Launch the interface
|
| 53 |
-
|
|
|
|
| 1 |
+
from openai import OpenAI
|
| 2 |
import gradio as gr
|
| 3 |
import os
|
| 4 |
|
| 5 |
+
client = OpenAI(
|
| 6 |
+
# This is the default and can be omitted
|
| 7 |
+
api_key=os.environ.get("OPENAI_API_KEY"),
|
| 8 |
+
)
|
| 9 |
+
|
| 10 |
+
def chat_response(prompt, history=[]):
|
| 11 |
+
"""
|
| 12 |
+
Provides a chatbot response based on the given prompt and history.
|
| 13 |
+
|
| 14 |
+
Args:
|
| 15 |
+
prompt (str): The user's input prompt.
|
| 16 |
+
history (list, optional): List of previous interactions (user and assistant messages). Defaults to [].
|
| 17 |
+
|
| 18 |
+
Returns:
|
| 19 |
+
str: The assistant's response.
|
| 20 |
+
"""
|
| 21 |
+
|
| 22 |
+
# Update history with the user's prompt
|
| 23 |
+
history.append({"role": "user", "content": prompt})
|
| 24 |
+
|
| 25 |
+
# Generate response using OpenAI's chat completion API
|
| 26 |
+
response = client.chat.completions.create(
|
| 27 |
+
model="gpt-3.5-turbo",
|
| 28 |
+
messages=[{"role": "user", "content": prompt}],
|
| 29 |
+
max_tokens=150,
|
| 30 |
+
temperature=0.7,
|
| 31 |
+
top_p=1,
|
| 32 |
+
frequency_penalty=0,
|
| 33 |
+
presence_penalty=0.6,
|
| 34 |
+
stop=None,
|
| 35 |
+
n=1
|
| 36 |
+
)
|
| 37 |
+
assistant_response = response.choices[0].message.content
|
| 38 |
+
|
| 39 |
+
# Update history with the assistant's response
|
| 40 |
+
history.append({"role": "assistant", "content": assistant_response})
|
| 41 |
+
|
| 42 |
+
return assistant_response, history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
# Create the Gradio interface
|
| 45 |
+
interface = gr.Interface(
|
| 46 |
+
fn=chat_response,
|
| 47 |
+
inputs=gr.Textbox(lines=3, label="Enter your prompt:"),
|
| 48 |
+
outputs="text",
|
| 49 |
+
title="OpenAI Chatbot: Ask Me Anything!",
|
| 50 |
+
#theme="dark",
|
| 51 |
+
#use_legacy_return=True, # Ensure all outputs are returned to Gradio
|
| 52 |
+
)
|
| 53 |
|
| 54 |
# Launch the interface
|
| 55 |
+
interface.launch()
|