abanm commited on
Commit
aae2b5a
·
verified ·
1 Parent(s): 207293e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +55 -7
app.py CHANGED
@@ -1,5 +1,51 @@
1
  import streamlit as st
2
- from api_helper import call_api # Keep the original API logic
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
 
4
  # Streamlit Configurations (must come first)
5
  st.set_page_config(page_title="💬 DUBSChat", layout="centered")
@@ -31,10 +77,12 @@ if prompt := st.chat_input():
31
  st.session_state["messages"].append({"role": "user", "content": prompt})
32
  st.chat_message("user").write(prompt)
33
 
34
- with st.spinner("Translating to Dubs language(Woof Woof!)"):
35
- # Call the original API logic to get the response
36
- response = call_api(prompt)
 
 
 
37
 
38
- # Append the assistant's response to the chat history
39
- st.session_state["messages"].append({"role": "assistant", "content": response})
40
- st.chat_message("assistant").write(response)
 
1
  import streamlit as st
2
+ import requests
3
+ import json
4
+
5
+ # Constants
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.
12
+
13
+ Args:
14
+ message (str): User's message.
15
+
16
+ Returns:
17
+ str: Streamed response from the backend API or an error message.
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
24
+
25
+ try:
26
+ # Send request with streaming enabled
27
+ response = requests.post(SPACE_URL, json=payload, headers=headers, stream=True)
28
+ response.raise_for_status()
29
+
30
+ # Aggregate streamed response
31
+ bot_response = ""
32
+ for chunk in response.iter_lines(decode_unicode=True):
33
+ if chunk.strip(): # Ensure the chunk is not empty
34
+ try:
35
+ # Parse JSON chunk
36
+ data = json.loads(chunk)
37
+ if "response" in data:
38
+ bot_response += data["response"]
39
+ yield bot_response.strip()
40
+ except json.JSONDecodeError:
41
+ bot_response += "\nError decoding response chunk."
42
+
43
+ if not bot_response:
44
+ yield "Error: No response from API."
45
+ except requests.exceptions.Timeout:
46
+ yield "Error: The API call timed out."
47
+ except requests.exceptions.RequestException as e:
48
+ yield f"Error: {str(e)}"
49
 
50
  # Streamlit Configurations (must come first)
51
  st.set_page_config(page_title="💬 DUBSChat", layout="centered")
 
77
  st.session_state["messages"].append({"role": "user", "content": prompt})
78
  st.chat_message("user").write(prompt)
79
 
80
+ with st.spinner("Translating to Dubs language (Woof Woof!)"):
81
+ # Call the API logic to get the response with streaming
82
+ bot_response = ""
83
+ for partial_response in call_api(prompt):
84
+ bot_response = partial_response
85
+ st.chat_message("assistant").write(bot_response)
86
 
87
+ # Append the final assistant's response to the chat history
88
+ st.session_state["messages"].append({"role": "assistant", "content": bot_response})