Update app.py
Browse files
app.py
CHANGED
|
@@ -2,9 +2,12 @@ import aiohttp
|
|
| 2 |
import asyncio
|
| 3 |
import json
|
| 4 |
import gradio as gr
|
|
|
|
| 5 |
|
| 6 |
# Define the API URLs
|
| 7 |
-
BASE_URL =
|
|
|
|
|
|
|
| 8 |
TOKEN_URL = BASE_URL + "/get-token"
|
| 9 |
CHAT_URL = BASE_URL + "/conversation"
|
| 10 |
|
|
@@ -51,7 +54,6 @@ async def chat(messList):
|
|
| 51 |
messHistory.append({"role": "assistant", "content": fullmessage}) # Append assistant response
|
| 52 |
return fullmessage
|
| 53 |
|
| 54 |
-
|
| 55 |
def gradio_chat(user_input, mode):
|
| 56 |
"""Synchronous wrapper for the async chat function, integrated with Gradio."""
|
| 57 |
messHistory.append({"role": "user", "content": f"[{mode}] {user_input}"})
|
|
@@ -60,20 +62,22 @@ def gradio_chat(user_input, mode):
|
|
| 60 |
assistant_response = loop.run_until_complete(chat(messHistory))
|
| 61 |
return assistant_response
|
| 62 |
|
| 63 |
-
|
| 64 |
# Gradio interface for user interaction
|
|
|
|
|
|
|
|
|
|
| 65 |
with gr.Blocks() as demo:
|
| 66 |
gr.Markdown("# Chat with AI")
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
# Launch the Gradio app
|
| 79 |
demo.launch()
|
|
|
|
| 2 |
import asyncio
|
| 3 |
import json
|
| 4 |
import gradio as gr
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
# Define the API URLs
|
| 8 |
+
BASE_URL = os.getenv("URL") # Ensure the environment variable "URL" is set
|
| 9 |
+
if not BASE_URL:
|
| 10 |
+
raise ValueError("Environment variable 'URL' not set")
|
| 11 |
TOKEN_URL = BASE_URL + "/get-token"
|
| 12 |
CHAT_URL = BASE_URL + "/conversation"
|
| 13 |
|
|
|
|
| 54 |
messHistory.append({"role": "assistant", "content": fullmessage}) # Append assistant response
|
| 55 |
return fullmessage
|
| 56 |
|
|
|
|
| 57 |
def gradio_chat(user_input, mode):
|
| 58 |
"""Synchronous wrapper for the async chat function, integrated with Gradio."""
|
| 59 |
messHistory.append({"role": "user", "content": f"[{mode}] {user_input}"})
|
|
|
|
| 62 |
assistant_response = loop.run_until_complete(chat(messHistory))
|
| 63 |
return assistant_response
|
| 64 |
|
|
|
|
| 65 |
# Gradio interface for user interaction
|
| 66 |
+
def chat_interface(user_input, mode):
|
| 67 |
+
return gradio_chat(user_input, mode)
|
| 68 |
+
|
| 69 |
with gr.Blocks() as demo:
|
| 70 |
gr.Markdown("# Chat with AI")
|
| 71 |
+
|
| 72 |
+
with gr.Row():
|
| 73 |
+
radio_mode = gr.Radio(["Friendly", "Formal", "Humorous"], label="Chat Mode", value="Friendly")
|
| 74 |
+
|
| 75 |
+
with gr.Row():
|
| 76 |
+
chatbot = gr.Interface(
|
| 77 |
+
fn=lambda user_input: chat_interface(user_input, radio_mode.value),
|
| 78 |
+
inputs=[gr.Textbox(label="Your message")],
|
| 79 |
+
outputs=[gr.Textbox(label="Assistant response")]
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
# Launch the Gradio app
|
| 83 |
demo.launch()
|