File size: 1,544 Bytes
be043a6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import requests

# Define the URL of the FastAPI service
API_URL = "http://localhost:8000/most_similar"  # Adjust if the FastAPI service is hosted elsewhere


def get_similar_prompts(query, n):
    try:
        response = requests.post(API_URL, json={"query": query, "n": n})
        response.raise_for_status()  # Raise an exception for HTTP errors
        return response.json()
    except requests.RequestException as e:
        st.error(f"Error: {e}")
        return None


def main():
    st.title("Prompt Similarity Finder")

    # User input for query
    query = st.text_input("Enter your query:", "")
    n = st.slider(
        "Number of similar prompts to retrieve:", min_value=1, max_value=20, value=5
    )

    if st.button("Find Similar Prompts"):
        if query:
            with st.spinner("Fetching similar prompts..."):
                result = get_similar_prompts(query, n)
                if result:
                    similar_prompts = result.get("similar_prompts", [])
                    if similar_prompts:
                        st.subheader("Similar Prompts:")
                        for item in similar_prompts:
                            st.write(f"**Score:** {item['score']:.2f}")
                            st.write(f"**Prompt:** {item['prompt']}")
                            st.write("---")
                    else:
                        st.write("No similar prompts found.")
        else:
            st.warning("Please enter a query.")


if __name__ == "__main__":
    main()