LivanArzuaga commited on
Commit
78f71d4
verified
1 Parent(s): d44b06c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -6
app.py CHANGED
@@ -2,25 +2,54 @@ import streamlit as st
2
  import requests
3
  import os
4
  import json
 
 
 
 
 
 
 
 
 
 
 
5
 
6
  # Configuraci贸n de la API de Flowise
7
- API_URL = os.getenv("API_URL", "https://livanarzuaga-test.hf.space/api/v1/prediction/2a2e535d-1807-4701-8468-c0ede26bcdbf")
 
 
 
 
 
8
 
9
  def query(payload, api_url=API_URL):
10
  try:
11
- response = requests.post(api_url, json=payload)
12
  response.raise_for_status()
13
  return response.json()
14
  except requests.exceptions.HTTPError as http_err:
15
- return {"error": f"HTTP error occurred: {http_err}"}
 
16
  except requests.exceptions.ConnectionError as conn_err:
17
- return {"error": f"Connection error occurred: {conn_err}"}
 
18
  except requests.exceptions.Timeout as timeout_err:
19
- return {"error": f"Timeout error occurred: {timeout_err}"}
 
20
  except requests.exceptions.RequestException as req_err:
21
- return {"error": f"An error occurred: {req_err}"}
 
 
 
 
 
 
22
 
23
  def get_response(user_input, api_url=API_URL):
 
 
 
 
24
  payload = {"question": user_input}
25
  response = query(payload, api_url)
26
  return response.get('text', "Error: No API response.")
@@ -35,6 +64,7 @@ def export_conversation(past, generated, file_path="conversation.json"):
35
  json.dump(conversation, f)
36
  return True
37
  except Exception as e:
 
38
  return str(e)
39
 
40
  # Inicializar claves en st.session_state
 
2
  import requests
3
  import os
4
  import json
5
+ from dotenv import load_dotenv
6
+ import logging
7
+ from dotenv import load_dotenv
8
+ from requests.adapters import HTTPAdapter
9
+ from requests.packages.urllib3.util.retry import Retry
10
+
11
+ # Configurar logging
12
+ logging.basicConfig(level=logging.INFO)
13
+
14
+ # Cargar variables de entorno
15
+ load_dotenv()
16
 
17
  # Configuraci贸n de la API de Flowise
18
+ API_URL = os.getenv("API_URL", "https://livanarzuaga-test.hf.space/api/v1/prediction/2dba91d7-15b7-4475-b0ec-34a11ffbeab5")
19
+ session = requests.Session()
20
+
21
+ # Configurar reintentos
22
+ retries = Retry(total=5, backoff_factor=1, status_forcelist=[502, 503, 504])
23
+ session.mount('https://', HTTPAdapter(max_retries=retries))
24
 
25
  def query(payload, api_url=API_URL):
26
  try:
27
+ response = session.post(api_url, json=payload)
28
  response.raise_for_status()
29
  return response.json()
30
  except requests.exceptions.HTTPError as http_err:
31
+ logging.error(f"HTTP error occurred: {http_err}")
32
+ return {"error": "HTTP error occurred. Please try again later."}
33
  except requests.exceptions.ConnectionError as conn_err:
34
+ logging.error(f"Connection error occurred: {conn_err}")
35
+ return {"error": "Connection error occurred. Please try again later."}
36
  except requests.exceptions.Timeout as timeout_err:
37
+ logging.error(f"Timeout error occurred: {timeout_err}")
38
+ return {"error": "Timeout error occurred. Please try again later."}
39
  except requests.exceptions.RequestException as req_err:
40
+ logging.error(f"An error occurred: {req_err}")
41
+ return {"error": "An error occurred. Please try again later."}
42
+
43
+ def validate_input(user_input):
44
+ if not user_input or len(user_input.strip()) == 0:
45
+ return False, "Input cannot be empty."
46
+ return True, ""
47
 
48
  def get_response(user_input, api_url=API_URL):
49
+ is_valid, error_message = validate_input(user_input)
50
+ if not is_valid:
51
+ return error_message
52
+
53
  payload = {"question": user_input}
54
  response = query(payload, api_url)
55
  return response.get('text', "Error: No API response.")
 
64
  json.dump(conversation, f)
65
  return True
66
  except Exception as e:
67
+ logging.error(f"Error exporting conversation: {e}")
68
  return str(e)
69
 
70
  # Inicializar claves en st.session_state