import streamlit as st import requests st.title("Prompt Search Engine") st.write("Enter a prompt to find the top *n* similar prompts.") # User input prompt prompt = st.text_input("Enter your prompt here:") # Input for n n = st.number_input("Number of similar prompts (n):", min_value=1, max_value=30, value=5, step=1) if st.button("Search"): # Make sure the prompt is not empty if prompt.strip() == "": st.warning("Please enter a prompt.") else: # API endpoint api_url = "https://anja97-prompt-search-engine.hf.space/search" # Prepare the payload payload = { "query": prompt, "n": int(n) } # Send the POST request try: with st.spinner('Searching for similar prompts...'): response = requests.post(api_url, json=payload, timeout=30) # Check if the request was successful if response.status_code == 200: data = response.json() results = data.get("results", []) if results: # Display the results st.success(f"Top {n} similar prompts:") for i, item in enumerate(results, start=1): score = item.get("score", 0) similar_prompt = item.get("prompt", "") st.markdown(f"### Result {i}") st.write(f"**Similarity score:** {score:.4f}") st.write(f"**Prompt:** {similar_prompt}") st.write("---") else: st.info("No similar prompts found.") else: st.error(f"API Error: {response.status_code} - {response.text}") except requests.exceptions.Timeout: st.error("The request timed out. Please try again later.") except Exception as e: st.error(f"An error occurred: {e}")