Spaces:
Build error
Build error
Commit
·
6762a06
1
Parent(s):
a729af0
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,11 @@ import time
|
|
| 4 |
|
| 5 |
import openai
|
| 6 |
from dotenv import load_dotenv
|
|
|
|
|
|
|
| 7 |
load_dotenv()
|
|
|
|
|
|
|
| 8 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 9 |
|
| 10 |
import gradio as gr
|
|
@@ -14,12 +18,16 @@ messages = []
|
|
| 14 |
|
| 15 |
# Function to add user's text to chat history
|
| 16 |
def add_user_text(chat_history, user_text):
|
|
|
|
| 17 |
print('user_text_from_typing: ', user_text)
|
| 18 |
|
| 19 |
global messages
|
|
|
|
| 20 |
messages += [{"role":'user', 'content': user_text}]
|
| 21 |
|
|
|
|
| 22 |
chat_history = chat_history + [(user_text, None)]
|
|
|
|
| 23 |
return chat_history, gr.update(value="", interactive=False)
|
| 24 |
|
| 25 |
# Function for the bot to respond
|
|
@@ -35,11 +43,13 @@ def bot_respond(chat_history, openai_gpt_key, model_choice):
|
|
| 35 |
messages=messages,
|
| 36 |
)
|
| 37 |
bot_text = bot_response["choices"][0]["message"]["content"]
|
|
|
|
| 38 |
print("bot_text: ", bot_text)
|
| 39 |
|
|
|
|
| 40 |
messages = messages + [{"role":'assistant', 'content': bot_text}]
|
| 41 |
|
| 42 |
-
|
| 43 |
chat_history[-1][1] = ""
|
| 44 |
|
| 45 |
# Yield the chat history with the bot's response character by character
|
|
@@ -50,12 +60,20 @@ def bot_respond(chat_history, openai_gpt_key, model_choice):
|
|
| 50 |
|
| 51 |
# Create a Gradio interface
|
| 52 |
with gr.Blocks() as demo:
|
|
|
|
| 53 |
openai_gpt_key = gr.Textbox(label="OpenAI GPT API Key", value="", placeholder="sk..")
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
clear_btn = gr.Button("Clear for Restart")
|
|
|
|
|
|
|
| 56 |
chat_history = gr.Chatbot([], elem_id="chat_history").style(height=500)
|
| 57 |
|
| 58 |
with gr.Box():
|
|
|
|
| 59 |
user_text = gr.Textbox(
|
| 60 |
show_label=False,
|
| 61 |
placeholder="Enter text and press enter",
|
|
@@ -67,8 +85,11 @@ with gr.Blocks() as demo:
|
|
| 67 |
bot_respond, [chat_history, openai_gpt_key, model_choice], chat_history).then(
|
| 68 |
lambda: gr.update(interactive=True), None, [user_text], queue=False)
|
| 69 |
|
|
|
|
| 70 |
clear_btn.click(lambda: None, None, chat_history, queue=False)
|
| 71 |
|
| 72 |
if __name__ == "__main__":
|
|
|
|
| 73 |
demo.queue()
|
|
|
|
| 74 |
demo.launch()
|
|
|
|
| 4 |
|
| 5 |
import openai
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
+
|
| 8 |
+
# Load environment variables from .env file
|
| 9 |
load_dotenv()
|
| 10 |
+
|
| 11 |
+
# Set OpenAI API key from environment variable
|
| 12 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 13 |
|
| 14 |
import gradio as gr
|
|
|
|
| 18 |
|
| 19 |
# Function to add user's text to chat history
|
| 20 |
def add_user_text(chat_history, user_text):
|
| 21 |
+
# Print the user's text from typing
|
| 22 |
print('user_text_from_typing: ', user_text)
|
| 23 |
|
| 24 |
global messages
|
| 25 |
+
# Add user's text to the messages list with 'user' role
|
| 26 |
messages += [{"role":'user', 'content': user_text}]
|
| 27 |
|
| 28 |
+
# Add user's text to the chat history
|
| 29 |
chat_history = chat_history + [(user_text, None)]
|
| 30 |
+
# Return updated chat history and update the display without interaction
|
| 31 |
return chat_history, gr.update(value="", interactive=False)
|
| 32 |
|
| 33 |
# Function for the bot to respond
|
|
|
|
| 43 |
messages=messages,
|
| 44 |
)
|
| 45 |
bot_text = bot_response["choices"][0]["message"]["content"]
|
| 46 |
+
# Print the bot's response
|
| 47 |
print("bot_text: ", bot_text)
|
| 48 |
|
| 49 |
+
# Add bot's response to the messages list with 'assistant' role
|
| 50 |
messages = messages + [{"role":'assistant', 'content': bot_text}]
|
| 51 |
|
| 52 |
+
# Clear the last entry in the chat history
|
| 53 |
chat_history[-1][1] = ""
|
| 54 |
|
| 55 |
# Yield the chat history with the bot's response character by character
|
|
|
|
| 60 |
|
| 61 |
# Create a Gradio interface
|
| 62 |
with gr.Blocks() as demo:
|
| 63 |
+
# Textbox for OpenAI GPT API Key
|
| 64 |
openai_gpt_key = gr.Textbox(label="OpenAI GPT API Key", value="", placeholder="sk..")
|
| 65 |
+
|
| 66 |
+
# Dropdown menu for selecting the model
|
| 67 |
+
model_choice = gr.Dropdown(label="Model Options", choices=['gpt-3.5-turbo', 'gpt-4'])
|
| 68 |
+
|
| 69 |
+
# Button to clear the chat history and restart
|
| 70 |
clear_btn = gr.Button("Clear for Restart")
|
| 71 |
+
|
| 72 |
+
# Chat history display
|
| 73 |
chat_history = gr.Chatbot([], elem_id="chat_history").style(height=500)
|
| 74 |
|
| 75 |
with gr.Box():
|
| 76 |
+
# Textbox for user input
|
| 77 |
user_text = gr.Textbox(
|
| 78 |
show_label=False,
|
| 79 |
placeholder="Enter text and press enter",
|
|
|
|
| 85 |
bot_respond, [chat_history, openai_gpt_key, model_choice], chat_history).then(
|
| 86 |
lambda: gr.update(interactive=True), None, [user_text], queue=False)
|
| 87 |
|
| 88 |
+
# Clear button click event
|
| 89 |
clear_btn.click(lambda: None, None, chat_history, queue=False)
|
| 90 |
|
| 91 |
if __name__ == "__main__":
|
| 92 |
+
# Queue the Gradio interface
|
| 93 |
demo.queue()
|
| 94 |
+
# Launch the Gradio interface
|
| 95 |
demo.launch()
|