abanm commited on
Commit
df4c020
·
verified ·
1 Parent(s): 2e17d47

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -20
app.py CHANGED
@@ -6,6 +6,19 @@ import json
6
  SPACE_URL = "https://abanm-dubs.hf.space/api/generate"
7
  API_KEY = "s3cr3t_k3y" # Replace with your actual API key
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
  def call_api(message):
10
  """
11
  Calls the backend API with the user's input using streaming.
@@ -13,11 +26,11 @@ def call_api(message):
13
  Args:
14
  message (str): User's message.
15
 
16
- Yields:
17
- str: Streamed partial response from the backend API.
18
  """
19
  headers = {
20
- "Authorization": f"Bearer {API_KEY}",
21
  "Content-Type": "application/json",
22
  }
23
  payload = {"prompt": message} # Match the API's expected input key
@@ -27,20 +40,23 @@ def call_api(message):
27
  response = requests.post(SPACE_URL, json=payload, headers=headers, stream=True)
28
  response.raise_for_status()
29
 
30
- # Stream response chunks
 
31
  for chunk in response.iter_lines(decode_unicode=True):
32
  if chunk.strip(): # Ensure the chunk is not empty
33
  try:
34
  # Parse JSON chunk
35
  data = json.loads(chunk)
36
  if "response" in data:
37
- yield data["response"]
38
  except json.JSONDecodeError:
39
- yield "\nError decoding response chunk."
 
 
40
  except requests.exceptions.Timeout:
41
- yield "Error: The API call timed out."
42
  except requests.exceptions.RequestException as e:
43
- yield f"Error: {str(e)}"
44
 
45
  # Streamlit Configurations (must come first)
46
  st.set_page_config(page_title="💬 DUBSChat", layout="centered")
@@ -56,7 +72,7 @@ if "messages" not in st.session_state:
56
  st.session_state["messages"] = [{"role": "assistant", "content": "Hello! How can I assist you today?"}]
57
 
58
  # Main Chat UI
59
- st.title("DubsChat")
60
  st.markdown("Empowering you with a Sustainable AI")
61
 
62
  # Chat Display
@@ -75,17 +91,12 @@ if prompt := st.chat_input():
75
  # Display a placeholder for the assistant's response
76
  assistant_message_placeholder = st.chat_message("assistant").empty()
77
 
78
- bot_response = ""
79
- first_chunk = True
80
-
81
- for partial_response in call_api(prompt):
82
- bot_response += partial_response
83
-
84
- if first_chunk:
85
- first_chunk = False
86
- st.spinner("Translating from Dubs language (Woof Woof!)")
87
-
88
- assistant_message_placeholder.write(bot_response) # Update incrementally
89
 
90
  # Append the final assistant's response to the chat history
91
  st.session_state["messages"].append({"role": "assistant", "content": bot_response})
 
 
6
  SPACE_URL = "https://abanm-dubs.hf.space/api/generate"
7
  API_KEY = "s3cr3t_k3y" # Replace with your actual API key
8
 
9
+
10
+ # Streamlit Configurations (must come first)
11
+ st.set_page_config(page_title="💬 DUBSChat", layout="centered")
12
+
13
+ # Sidebar Configuration
14
+ with st.sidebar:
15
+ dubs_key = st.text_input("Enter Dubs Key", key="chatbot_api_key", type="password")
16
+ st.markdown("Dubs Recall")
17
+ #st.markdown("""Other Features.""")
18
+ st.markdown("""Comming Soon!""")
19
+
20
+
21
+
22
  def call_api(message):
23
  """
24
  Calls the backend API with the user's input using streaming.
 
26
  Args:
27
  message (str): User's message.
28
 
29
+ Returns:
30
+ str: Streamed response from the backend API or an error message.
31
  """
32
  headers = {
33
+ "Authorization": f"Bearer {dubs_key}",
34
  "Content-Type": "application/json",
35
  }
36
  payload = {"prompt": message} # Match the API's expected input key
 
40
  response = requests.post(SPACE_URL, json=payload, headers=headers, stream=True)
41
  response.raise_for_status()
42
 
43
+ # Aggregate streamed response
44
+ bot_response = ""
45
  for chunk in response.iter_lines(decode_unicode=True):
46
  if chunk.strip(): # Ensure the chunk is not empty
47
  try:
48
  # Parse JSON chunk
49
  data = json.loads(chunk)
50
  if "response" in data:
51
+ bot_response += data["response"]
52
  except json.JSONDecodeError:
53
+ bot_response += "\nError decoding response chunk."
54
+
55
+ return bot_response.strip() if bot_response else "Error: No response from API."
56
  except requests.exceptions.Timeout:
57
+ return "Error: The API call timed out."
58
  except requests.exceptions.RequestException as e:
59
+ return f"Error: {str(e)}"
60
 
61
  # Streamlit Configurations (must come first)
62
  st.set_page_config(page_title="💬 DUBSChat", layout="centered")
 
72
  st.session_state["messages"] = [{"role": "assistant", "content": "Hello! How can I assist you today?"}]
73
 
74
  # Main Chat UI
75
+ st.title("DUBS v.0.0.3")
76
  st.markdown("Empowering you with a Sustainable AI")
77
 
78
  # Chat Display
 
91
  # Display a placeholder for the assistant's response
92
  assistant_message_placeholder = st.chat_message("assistant").empty()
93
 
94
+ with st.spinner("Translating Dubs language (Woof Woof!)"):
95
+ bot_response = ""
96
+ for partial_response in call_api(prompt):
97
+ bot_response += partial_response
98
+ assistant_message_placeholder.write(bot_response) # Update incrementally
 
 
 
 
 
 
99
 
100
  # Append the final assistant's response to the chat history
101
  st.session_state["messages"].append({"role": "assistant", "content": bot_response})
102
+