abanm commited on
Commit
ccef877
·
verified ·
1 Parent(s): 0a37a52

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -18
app.py CHANGED
@@ -6,7 +6,6 @@ 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
-
10
  # Streamlit Configurations (must come first)
11
  st.set_page_config(page_title="💬 DUBSChat", layout="centered")
12
 
@@ -14,10 +13,7 @@ st.set_page_config(page_title="💬 DUBSChat", layout="centered")
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
  """
@@ -26,8 +22,8 @@ def call_api(message):
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}",
@@ -40,8 +36,6 @@ def call_api(message):
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:
@@ -50,13 +44,11 @@ def call_api(message):
50
  if "response" in data:
51
  yield 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
  # Chat History Initialization
62
  if "messages" not in st.session_state:
@@ -81,9 +73,12 @@ if prompt := st.chat_input():
81
 
82
  with st.spinner("Translating Dubs language (Woof Woof!)"):
83
  assistant_message_placeholder = st.chat_message("assistant").empty()
84
- response_stream = call_api(prompt)
85
- assistant_message_placeholder.write_stream(response_stream)
 
 
 
 
 
86
 
87
- # Append the final assistant's response to the chat history
88
- st.session_state["messages"].append({"role": "assistant", "content": response_stream})
89
 
 
6
  SPACE_URL = "https://abanm-dubs.hf.space/api/generate"
7
  API_KEY = "s3cr3t_k3y" # Replace with your actual API key
8
 
 
9
  # Streamlit Configurations (must come first)
10
  st.set_page_config(page_title="💬 DUBSChat", layout="centered")
11
 
 
13
  with st.sidebar:
14
  dubs_key = st.text_input("Enter Dubs Key", key="chatbot_api_key", type="password")
15
  st.markdown("Dubs Recall")
16
+ st.markdown("""Coming Soon!""")
 
 
 
17
 
18
  def call_api(message):
19
  """
 
22
  Args:
23
  message (str): User's message.
24
 
25
+ Yields:
26
+ str: Streamed response chunks from the backend API.
27
  """
28
  headers = {
29
  "Authorization": f"Bearer {dubs_key}",
 
36
  response = requests.post(SPACE_URL, json=payload, headers=headers, stream=True)
37
  response.raise_for_status()
38
 
 
 
39
  for chunk in response.iter_lines(decode_unicode=True):
40
  if chunk.strip(): # Ensure the chunk is not empty
41
  try:
 
44
  if "response" in data:
45
  yield data["response"]
46
  except json.JSONDecodeError:
47
+ yield "Error decoding response chunk."
 
 
48
  except requests.exceptions.Timeout:
49
+ yield "Error: The API call timed out."
50
  except requests.exceptions.RequestException as e:
51
+ yield f"Error: {str(e)}"
52
 
53
  # Chat History Initialization
54
  if "messages" not in st.session_state:
 
73
 
74
  with st.spinner("Translating Dubs language (Woof Woof!)"):
75
  assistant_message_placeholder = st.chat_message("assistant").empty()
76
+ full_response = "" # To accumulate the entire response
77
+ for response_chunk in call_api(prompt):
78
+ full_response += response_chunk
79
+ assistant_message_placeholder.write(full_response) # Update progressively
80
+
81
+ # Append the final assistant's response to the chat history
82
+ st.session_state["messages"].append({"role": "assistant", "content": full_response})
83
 
 
 
84