Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
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.")
|
| 27 |
+
|
| 28 |
+
def export_conversation(past, generated, file_path="conversation.json"):
|
| 29 |
+
try:
|
| 30 |
+
conversation = {
|
| 31 |
+
"past": past,
|
| 32 |
+
"generated": generated
|
| 33 |
+
}
|
| 34 |
+
with open(file_path, "w") as f:
|
| 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
|
| 41 |
+
if 'history' not in st.session_state:
|
| 42 |
+
st.session_state['history'] = []
|
| 43 |
+
|
| 44 |
+
if 'generated' not in st.session_state:
|
| 45 |
+
st.session_state['generated'] = ["Hi I can answer your weather related questions"]
|
| 46 |
+
|
| 47 |
+
if 'past' not in st.session_state:
|
| 48 |
+
st.session_state['past'] = ["Hello"]
|
| 49 |
+
|
| 50 |
+
# Sidebar con opciones
|
| 51 |
+
st.sidebar.title("Opciones")
|
| 52 |
+
if st.sidebar.button("Nueva conversaci贸n"):
|
| 53 |
+
st.session_state['past'] = ["Hello"]
|
| 54 |
+
st.session_state['generated'] = ["Hi I can answer your weather related questions"]
|
| 55 |
+
st.session_state['history'] = []
|
| 56 |
+
st.sidebar.success("Nueva conversaci贸n iniciada")
|
| 57 |
+
|
| 58 |
+
if st.sidebar.button("Exportar conversaci贸n"):
|
| 59 |
+
result = export_conversation(st.session_state['past'], st.session_state['generated'])
|
| 60 |
+
if result is True:
|
| 61 |
+
st.sidebar.success("Conversaci贸n exportada como conversation.json")
|
| 62 |
+
else:
|
| 63 |
+
st.sidebar.error(f"Error al exportar la conversaci贸n: {result}")
|
| 64 |
+
|
| 65 |
+
# Contenedor para el historial del chat
|
| 66 |
+
chat_container = st.empty()
|
| 67 |
+
|
| 68 |
+
# Contenedor para la entrada de texto del usuario
|
| 69 |
+
with st.form(key='my_form', clear_on_submit=True):
|
| 70 |
+
user_input = st.text_input("Chat:", placeholder="Type here to chat", key='input')
|
| 71 |
+
submit_button = st.form_submit_button(label='Send')
|
| 72 |
+
|
| 73 |
+
if submit_button and user_input:
|
| 74 |
+
output = get_response(user_input)
|
| 75 |
+
st.session_state['past'].append(user_input)
|
| 76 |
+
st.session_state['generated'].append(output)
|
| 77 |
+
|
| 78 |
+
# Mostrar el historial del chat
|
| 79 |
+
with chat_container.container():
|
| 80 |
+
for i in range(len(st.session_state['generated'])):
|
| 81 |
+
st.write(f"**User:** {st.session_state['past'][i]}")
|
| 82 |
+
st.write(f"**Bot:** {st.session_state['generated'][i]}")
|