abanm commited on
Commit
cfde60b
·
verified ·
1 Parent(s): 381273f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -5
app.py CHANGED
@@ -9,9 +9,9 @@ API_URL = "https://z7svds7k42bwhhgm.us-east-1.aws.endpoints.huggingface.cloud"
9
  HF_API_KEY = os.getenv("HF_API_KEY") # Retrieve the Hugging Face API key from system variables
10
  EOS_TOKEN = "<|end|>"
11
  CHAT_HISTORY_DIR = "chat_histories"
12
- IMAGE_PATH = "DubsChat.png"
13
  IMAGE_PATH_2 = "Reboot AI.png"
14
- Dubs_PATH = "Dubs.png"
15
 
16
  # Ensure the directory exists
17
  try:
@@ -34,10 +34,12 @@ def query(payload):
34
  }
35
  try:
36
  response = requests.post(API_URL, headers=headers, json=payload)
37
- response.raise_for_status()
38
  return response.json()
39
  except requests.exceptions.RequestException as e:
40
- return {"error": str(e)}
 
 
41
 
42
  def save_chat_history(session_name, messages):
43
  file_path = os.path.join(CHAT_HISTORY_DIR, f"{session_name}.json")
@@ -124,7 +126,11 @@ if prompt := st.chat_input():
124
  }
125
  }
126
  response = query(payload)
127
- assistant_response = response.get("generated_text", "Error: Unable to fetch response.")
 
 
 
 
128
  assistant_message_placeholder.write(assistant_response)
129
 
130
  st.session_state["messages"].append({"role": "assistant", "content": assistant_response})
 
9
  HF_API_KEY = os.getenv("HF_API_KEY") # Retrieve the Hugging Face API key from system variables
10
  EOS_TOKEN = "<|end|>"
11
  CHAT_HISTORY_DIR = "chat_histories"
12
+ IMAGE_PATH = "DubsChat.png"
13
  IMAGE_PATH_2 = "Reboot AI.png"
14
+ Dubs_PATH = "Dubs.png"
15
 
16
  # Ensure the directory exists
17
  try:
 
34
  }
35
  try:
36
  response = requests.post(API_URL, headers=headers, json=payload)
37
+ response.raise_for_status() # Raise an error for bad responses (4xx, 5xx)
38
  return response.json()
39
  except requests.exceptions.RequestException as e:
40
+ return {"error": f"Request error: {e}"}
41
+ except json.JSONDecodeError:
42
+ return {"error": "Invalid JSON response from server"}
43
 
44
  def save_chat_history(session_name, messages):
45
  file_path = os.path.join(CHAT_HISTORY_DIR, f"{session_name}.json")
 
126
  }
127
  }
128
  response = query(payload)
129
+ if "error" in response:
130
+ assistant_response = response["error"]
131
+ else:
132
+ assistant_response = response.get("generated_text", "Error: No generated text in response.")
133
+
134
  assistant_message_placeholder.write(assistant_response)
135
 
136
  st.session_state["messages"].append({"role": "assistant", "content": assistant_response})