File size: 1,966 Bytes
0f44211
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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}")