File size: 1,419 Bytes
8d8c002
cd2ebf8
0ea4e58
8d8c002
8f8231b
8d8c002
 
 
cd2ebf8
 
255a045
0ea4e58
cd2ebf8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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.")