alexpantex's picture
Upload app.py with huggingface_hub
255a045 verified
raw
history blame
1.42 kB
import threading
import streamlit as st
import requests
from scripts.api import start_api_server
import os
# Start the API server in a separate thread
api_thread = threading.Thread(target=start_api_server)
api_thread.start()
# Use localhost as FastAPI and Streamlit run in the same environment on Spaces
API_PORT = os.getenv("PORT", "7861")
API_URL = f"http://localhost:{API_PORT}/search"
st.title("Prompt Search App")
st.write("Enter a query below to find similar prompts.")
query = st.text_input("Query:")
n_results = st.slider("Number of Results:", min_value=1, max_value=10, value=5)
if st.button("Search"):
if query.strip():
payload = {"query": query, "n_results": n_results}
try:
response = requests.post(API_URL, json=payload)
if response.status_code == 200:
data = response.json()
st.write(f"**Query:** {data['query']}")
st.write("**Similar Prompts:**")
for idx, result in enumerate(data["similar_queries"], 1):
st.write(f"{idx}. {result['prompt']} (Score: {result['score']:.4f})")
else:
st.error(f"API Error: {response.status_code} - {response.json().get('detail', 'No details available.')}")
except Exception as e:
st.error(f"Failed to connect to the API. Error: {e}")
else:
st.warning("Please enter a valid query.")