Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
def query(payload, api_url=API_URL):
|
| 10 |
try:
|
| 11 |
-
response =
|
| 12 |
response.raise_for_status()
|
| 13 |
return response.json()
|
| 14 |
except requests.exceptions.HTTPError as http_err:
|
| 15 |
-
|
|
|
|
| 16 |
except requests.exceptions.ConnectionError as conn_err:
|
| 17 |
-
|
|
|
|
| 18 |
except requests.exceptions.Timeout as timeout_err:
|
| 19 |
-
|
|
|
|
| 20 |
except requests.exceptions.RequestException as req_err:
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|