up
Browse files- app.py +26 -23
- requirements.txt +1 -3
app.py
CHANGED
|
@@ -2,39 +2,42 @@ import streamlit as st
|
|
| 2 |
from fastapi import FastAPI
|
| 3 |
from pydantic import BaseModel
|
| 4 |
import uvicorn
|
|
|
|
| 5 |
from fastapi.middleware.wsgi import WSGIMiddleware
|
| 6 |
|
| 7 |
-
# Crear aplicaci贸n Streamlit
|
| 8 |
st.title("Mi Amigo Virtual 馃")
|
| 9 |
-
st.write("Bienvenido a tu asistente virtual!")
|
| 10 |
|
| 11 |
-
#
|
|
|
|
|
|
|
|
|
|
| 12 |
class Message(BaseModel):
|
| 13 |
text: str
|
| 14 |
|
| 15 |
-
# Crear
|
| 16 |
app = FastAPI()
|
| 17 |
|
|
|
|
| 18 |
@app.post("/chat")
|
| 19 |
def chat(msg: Message):
|
| 20 |
-
return {"response": f"Hola
|
| 21 |
-
|
| 22 |
-
# Integrar FastAPI en el servidor Streamlit
|
| 23 |
-
from starlette.applications import Starlette
|
| 24 |
-
from starlette.routing import Route
|
| 25 |
-
|
| 26 |
-
# Configura el servidor WSGI para que ambas aplicaciones corran en el mismo servidor
|
| 27 |
-
server = Starlette(debug=True, routes=[
|
| 28 |
-
Route("/", endpoint=st.write) # Endpoint para Streamlit
|
| 29 |
-
])
|
| 30 |
|
| 31 |
-
#
|
| 32 |
-
app.mount("/fastapi", WSGIMiddleware(server))
|
| 33 |
-
|
| 34 |
-
# Ejecutar el servidor Uvicorn con FastAPI
|
| 35 |
def run_api():
|
| 36 |
-
uvicorn.run(app, host="0.0.0.0", port=
|
| 37 |
-
|
| 38 |
-
# Iniciar FastAPI en
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
from fastapi import FastAPI
|
| 3 |
from pydantic import BaseModel
|
| 4 |
import uvicorn
|
| 5 |
+
import threading
|
| 6 |
from fastapi.middleware.wsgi import WSGIMiddleware
|
| 7 |
|
| 8 |
+
# Crear la aplicaci贸n Streamlit
|
| 9 |
st.title("Mi Amigo Virtual 馃")
|
|
|
|
| 10 |
|
| 11 |
+
# Preguntar el nombre del usuario
|
| 12 |
+
name = st.text_input("驴Qui茅n eres?")
|
| 13 |
+
|
| 14 |
+
# Crear la clase de datos para FastAPI
|
| 15 |
class Message(BaseModel):
|
| 16 |
text: str
|
| 17 |
|
| 18 |
+
# Crear la aplicaci贸n FastAPI
|
| 19 |
app = FastAPI()
|
| 20 |
|
| 21 |
+
# Endpoint de la API para recibir el nombre y responder
|
| 22 |
@app.post("/chat")
|
| 23 |
def chat(msg: Message):
|
| 24 |
+
return {"response": f"隆Hola {msg.text}! Soy tu amigo virtual. 驴En qu茅 puedo ayudarte hoy?"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
+
# Crear la funci贸n que corre FastAPI en segundo plano
|
|
|
|
|
|
|
|
|
|
| 27 |
def run_api():
|
| 28 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
| 29 |
+
|
| 30 |
+
# Iniciar FastAPI en un hilo separado
|
| 31 |
+
threading.Thread(target=run_api, daemon=True).start()
|
| 32 |
+
|
| 33 |
+
# Si el usuario ha ingresado su nombre, se hace la petici贸n a la API
|
| 34 |
+
if name:
|
| 35 |
+
# Enviar el nombre a la API
|
| 36 |
+
import requests
|
| 37 |
+
response = requests.post("http://localhost:8000/chat", json={"text": name})
|
| 38 |
+
|
| 39 |
+
if response.status_code == 200:
|
| 40 |
+
# Mostrar la respuesta de la API
|
| 41 |
+
st.write(response.json()["response"])
|
| 42 |
+
else:
|
| 43 |
+
st.write("隆Hola! 驴C贸mo te llamas?")
|
requirements.txt
CHANGED
|
@@ -1,6 +1,4 @@
|
|
| 1 |
transformers
|
| 2 |
torch
|
| 3 |
streamlit
|
| 4 |
-
ffmpeg
|
| 5 |
-
fastapi
|
| 6 |
-
uvicorn
|
|
|
|
| 1 |
transformers
|
| 2 |
torch
|
| 3 |
streamlit
|
| 4 |
+
ffmpeg
|
|
|
|
|
|