Spaces:
Runtime error
Runtime error
Eric Michael Martinez
commited on
Commit
·
2010315
1
Parent(s):
fd85e62
update chatinterface
Browse files- app/app.py +24 -52
- requirements.txt +2 -2
app/app.py
CHANGED
@@ -2,13 +2,13 @@ from httpx import AsyncClient
|
|
2 |
import os
|
3 |
import requests
|
4 |
import gradio as gr
|
5 |
-
import openai
|
6 |
|
7 |
from fastapi import Depends, FastAPI, Request
|
8 |
from app.db import User, create_db_and_tables
|
9 |
from app.schemas import UserCreate, UserRead, UserUpdate
|
10 |
from app.users import auth_backend, current_active_user, fastapi_users
|
11 |
from dotenv import load_dotenv
|
|
|
12 |
import examples as chatbot_examples
|
13 |
|
14 |
# Get the current environment from the environment variable
|
@@ -24,6 +24,8 @@ elif current_environment == "prod":
|
|
24 |
else:
|
25 |
raise ValueError("Invalid environment specified")
|
26 |
|
|
|
|
|
27 |
|
28 |
def api_login(email, password):
|
29 |
port = os.getenv("APP_PORT")
|
@@ -119,9 +121,6 @@ def generate_image(prompt):
|
|
119 |
|
120 |
# Define a function to handle the chat interaction with the AI model
|
121 |
def chat(message, chatbot_messages, model, temperature, system_message):
|
122 |
-
# Initialize chatbot_messages
|
123 |
-
chatbot_messages = chatbot_messages or []
|
124 |
-
|
125 |
history_openai_format = []
|
126 |
for human, assistant in chatbot_messages:
|
127 |
history_openai_format.append({"role": "user", "content": human})
|
@@ -140,11 +139,8 @@ def chat(message, chatbot_messages, model, temperature, system_message):
|
|
140 |
# If an error occurs, raise a Gradio error
|
141 |
raise gr.Error(e)
|
142 |
|
143 |
-
# Append the user's message and the AI's reply to the chatbot_messages list
|
144 |
-
chatbot_messages.append((message, ai_reply))
|
145 |
-
|
146 |
# Return None (empty out the user's message textbox), the updated chatbot_messages
|
147 |
-
return
|
148 |
|
149 |
|
150 |
# Define a function to launch the chatbot interface using Gradio
|
@@ -169,55 +165,31 @@ def get_chatbot_app(additional_examples=[]):
|
|
169 |
with gr.Blocks() as app:
|
170 |
with gr.Tab("Conversation"):
|
171 |
with gr.Row():
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
max_lines=400,
|
185 |
-
)
|
186 |
-
with gr.Column():
|
187 |
-
with gr.Row():
|
188 |
-
# Create a dropdown to select the AI model
|
189 |
-
model_selector = gr.Dropdown(
|
190 |
["gpt-3.5-turbo", "gpt-3.5-turbo-16k"],
|
191 |
label="Model",
|
192 |
value="gpt-3.5-turbo",
|
193 |
-
)
|
194 |
-
|
195 |
-
label="Temperature",
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
# Create a button to send the user's message
|
202 |
-
btn = gr.Button(value="Send")
|
203 |
-
|
204 |
-
# Connect the example load button to the choose_example function
|
205 |
-
example_load_btn.click(
|
206 |
-
choose_example,
|
207 |
-
inputs=[example_dropdown],
|
208 |
-
outputs=[system_message, message, chatbot],
|
209 |
-
)
|
210 |
-
# Connect the send button to the chat function
|
211 |
-
btn.click(
|
212 |
-
chat,
|
213 |
-
inputs=[
|
214 |
-
message,
|
215 |
-
chatbot,
|
216 |
-
model_selector,
|
217 |
-
temperature_slider,
|
218 |
system_message,
|
219 |
],
|
220 |
-
outputs=[message, chatbot],
|
221 |
)
|
222 |
with gr.Tab("Image Generation"):
|
223 |
image_prompt = gr.Textbox(
|
|
|
2 |
import os
|
3 |
import requests
|
4 |
import gradio as gr
|
|
|
5 |
|
6 |
from fastapi import Depends, FastAPI, Request
|
7 |
from app.db import User, create_db_and_tables
|
8 |
from app.schemas import UserCreate, UserRead, UserUpdate
|
9 |
from app.users import auth_backend, current_active_user, fastapi_users
|
10 |
from dotenv import load_dotenv
|
11 |
+
|
12 |
import examples as chatbot_examples
|
13 |
|
14 |
# Get the current environment from the environment variable
|
|
|
24 |
else:
|
25 |
raise ValueError("Invalid environment specified")
|
26 |
|
27 |
+
import openai
|
28 |
+
|
29 |
|
30 |
def api_login(email, password):
|
31 |
port = os.getenv("APP_PORT")
|
|
|
121 |
|
122 |
# Define a function to handle the chat interaction with the AI model
|
123 |
def chat(message, chatbot_messages, model, temperature, system_message):
|
|
|
|
|
|
|
124 |
history_openai_format = []
|
125 |
for human, assistant in chatbot_messages:
|
126 |
history_openai_format.append({"role": "user", "content": human})
|
|
|
139 |
# If an error occurs, raise a Gradio error
|
140 |
raise gr.Error(e)
|
141 |
|
|
|
|
|
|
|
142 |
# Return None (empty out the user's message textbox), the updated chatbot_messages
|
143 |
+
return ai_reply
|
144 |
|
145 |
|
146 |
# Define a function to launch the chatbot interface using Gradio
|
|
|
165 |
with gr.Blocks() as app:
|
166 |
with gr.Tab("Conversation"):
|
167 |
with gr.Row():
|
168 |
+
# Create a textbox for the system message (prompt)
|
169 |
+
system_message = gr.TextArea(
|
170 |
+
label="System Message (Prompt)",
|
171 |
+
value="You are a helpful assistant.",
|
172 |
+
lines=20,
|
173 |
+
max_lines=400,
|
174 |
+
)
|
175 |
+
# Create a chatbot interface for the conversation
|
176 |
+
chatbot = gr.ChatInterface(
|
177 |
+
chat,
|
178 |
+
additional_inputs=[
|
179 |
+
gr.Dropdown(
|
|
|
|
|
|
|
|
|
|
|
|
|
180 |
["gpt-3.5-turbo", "gpt-3.5-turbo-16k"],
|
181 |
label="Model",
|
182 |
value="gpt-3.5-turbo",
|
183 |
+
),
|
184 |
+
gr.Slider(
|
185 |
+
label="Temperature",
|
186 |
+
minimum=0,
|
187 |
+
maximum=2,
|
188 |
+
step=0.1,
|
189 |
+
value=0,
|
190 |
+
),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
191 |
system_message,
|
192 |
],
|
|
|
193 |
)
|
194 |
with gr.Tab("Image Generation"):
|
195 |
image_prompt = gr.Textbox(
|
requirements.txt
CHANGED
@@ -1,10 +1,10 @@
|
|
1 |
fastapi
|
2 |
fastapi-users[sqlalchemy]
|
3 |
-
gradio
|
4 |
httpx
|
5 |
openai
|
6 |
python-dotenv
|
7 |
Requests
|
8 |
-
|
9 |
uvicorn
|
10 |
asyncpg
|
|
|
1 |
fastapi
|
2 |
fastapi-users[sqlalchemy]
|
3 |
+
gradio==3.44.4
|
4 |
httpx
|
5 |
openai
|
6 |
python-dotenv
|
7 |
Requests
|
8 |
+
sqlalchemy[asyncio]
|
9 |
uvicorn
|
10 |
asyncpg
|