Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import aiohttp
|
| 2 |
+
import asyncio
|
| 3 |
+
import json
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
# Define the API URLs
|
| 7 |
+
BASE_URL = "https://free-duck-duck-go-assist-coral.vercel.app/api"
|
| 8 |
+
TOKEN_URL = BASE_URL + "/get-token"
|
| 9 |
+
CHAT_URL = BASE_URL + "/conversation"
|
| 10 |
+
|
| 11 |
+
# Initialize the token and message history
|
| 12 |
+
token = ""
|
| 13 |
+
messHistory: list = []
|
| 14 |
+
|
| 15 |
+
async def chat(messList):
|
| 16 |
+
"""Async function to send and receive messages with the server."""
|
| 17 |
+
global token
|
| 18 |
+
async with aiohttp.ClientSession() as session:
|
| 19 |
+
# Request token if not already set
|
| 20 |
+
if token == "":
|
| 21 |
+
async with session.get(TOKEN_URL) as resp:
|
| 22 |
+
data = await resp.json()
|
| 23 |
+
token = data["token"]
|
| 24 |
+
|
| 25 |
+
body = {
|
| 26 |
+
"token": token,
|
| 27 |
+
"message": messList,
|
| 28 |
+
"stream": True
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
# Make the POST request to the chat API
|
| 32 |
+
async with session.post(CHAT_URL, json=body) as resp:
|
| 33 |
+
if resp.status != 200:
|
| 34 |
+
return "Error occurred during the chat process."
|
| 35 |
+
|
| 36 |
+
fullmessage = ""
|
| 37 |
+
# Stream and decode response chunks
|
| 38 |
+
async for chunk in resp.content.iter_any():
|
| 39 |
+
data_str = chunk.decode("utf-8")
|
| 40 |
+
json_str = data_str.replace("data: ", "")
|
| 41 |
+
if json_str.strip() == "[DONE]":
|
| 42 |
+
break
|
| 43 |
+
else:
|
| 44 |
+
try:
|
| 45 |
+
data_dict = json.loads(json_str)
|
| 46 |
+
fullmessage += data_dict["message"]
|
| 47 |
+
token = data_dict["resp_token"] # Update token
|
| 48 |
+
except KeyError:
|
| 49 |
+
pass
|
| 50 |
+
|
| 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}"})
|
| 58 |
+
loop = asyncio.new_event_loop()
|
| 59 |
+
asyncio.set_event_loop(loop)
|
| 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 |
+
chat_box = gr.ChatInterface(fn=gradio_chat, label="Assistant")
|
| 68 |
+
|
| 69 |
+
# Radio button for selecting modes
|
| 70 |
+
radio_mode = gr.Radio(["Friendly", "Formal", "Humorous"], label="Chat Mode", value="Friendly")
|
| 71 |
+
|
| 72 |
+
# Chatbot UI setup with mode and user input
|
| 73 |
+
chat_ui = gr.ChatInterface(fn=lambda user_input: gradio_chat(user_input, radio_mode.value))
|
| 74 |
+
|
| 75 |
+
# Link mode selection to chat interface
|
| 76 |
+
chat_box.submit(gradio_chat, [chat_box, radio_mode], chat_box)
|
| 77 |
+
|
| 78 |
+
# Launch the Gradio app
|
| 79 |
+
demo.launch()
|