ar08 commited on
Commit
1fe665e
·
verified ·
1 Parent(s): 76e1a7b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -14
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 = "https://free-duck-duck-go-assist-coral.vercel.app/api"
 
 
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
- 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()
 
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()