Spaces:
Runtime error
Runtime error
Vitomir Jovanović
commited on
Commit
•
e9fda99
1
Parent(s):
b2667d5
New Streamlit code for Hugging Face deployment
Browse files
app.py
CHANGED
@@ -1,34 +1,100 @@
|
|
1 |
-
# streamlit_app.py
|
2 |
import streamlit as st
|
3 |
-
import
|
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 |
-
if
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
else:
|
34 |
-
st.error(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from models.vectorizer import Vectorizer
|
3 |
+
from models.prompt_search_engine import PromptSearchEngine
|
4 |
+
from models.data_reader import load_prompts_from_jsonl
|
5 |
+
from models.Query import Query, SimilarPrompt, SearchResponse, PromptVector, VectorResponse
|
6 |
+
from sentence_transformers import SentenceTransformer
|
7 |
+
import os
|
8 |
+
|
9 |
+
# Path to your prompts data (you need to upload this file to your Hugging Face space)
|
10 |
+
prompt_path = "models/prompts_data.jsonl" # Update this to the correct path in your space
|
11 |
+
|
12 |
+
# Initialize search engine and model
|
13 |
+
prompts = load_prompts_from_jsonl(prompt_path)
|
14 |
+
search_engine = PromptSearchEngine()
|
15 |
+
search_engine.add_prompts_to_vector_database(prompts)
|
16 |
+
|
17 |
+
# Streamlit App Interface
|
18 |
+
st.title("Prompt Search Engine")
|
19 |
+
st.write("Search for similar prompts using the local search engine.")
|
20 |
+
|
21 |
+
# Input for the user's prompt
|
22 |
+
query_input = st.text_input("Enter your prompt:")
|
23 |
+
|
24 |
+
# Number of similar prompts to retrieve (k)
|
25 |
+
k = st.number_input("Number of similar prompts to retrieve:", min_value=1, max_value=10, value=3)
|
26 |
+
|
27 |
+
# Button to trigger search
|
28 |
+
if st.button("Search Prompts"):
|
29 |
+
if query_input:
|
30 |
+
query = Query(prompt=query_input)
|
31 |
+
similar_prompts, distances = search_engine.most_similar(query.prompt, top_k=k)
|
32 |
+
|
33 |
+
# Format and display search results
|
34 |
+
response = [
|
35 |
+
SimilarPrompt(prompt=prompt, distance=float(distance))
|
36 |
+
for prompt, distance in zip(similar_prompts, distances)
|
37 |
+
]
|
38 |
+
st.write("Search Results:")
|
39 |
+
for result in response:
|
40 |
+
st.write(f"Prompt: {result.prompt}, Distance: {result.distance}")
|
41 |
+
else:
|
42 |
+
st.error("Please enter a prompt.")
|
43 |
+
|
44 |
+
# Additional functionality for vector similarity
|
45 |
+
st.write("---")
|
46 |
+
st.write("### Vector Similarities")
|
47 |
+
|
48 |
+
if st.button("Retrieve All Vector Similarities"):
|
49 |
+
if query_input:
|
50 |
+
query = Query(prompt=query_input)
|
51 |
+
query_embedding = search_engine.model.encode([query.prompt]) # Encode the prompt to a vector
|
52 |
+
all_similarities = search_engine.cosine_similarity(query_embedding, search_engine.index)
|
53 |
+
|
54 |
+
# Format and display vector similarities
|
55 |
+
response = [
|
56 |
+
PromptVector(vector=index, distance=float(distance))
|
57 |
+
for index, distance in enumerate(all_similarities)
|
58 |
+
]
|
59 |
+
st.write("Vector Similarities:")
|
60 |
+
for result in response:
|
61 |
+
st.write(f"Vector Index: {result.vector}, Distance: {result.distance}")
|
62 |
else:
|
63 |
+
st.error("Please enter a prompt.")
|
64 |
+
|
65 |
+
|
66 |
+
|
67 |
+
# # streamlit_app.py
|
68 |
+
# import streamlit as st
|
69 |
+
# import requests
|
70 |
+
|
71 |
+
# # Streamlit app title
|
72 |
+
# st.title("Top K Search with Vector DataBase")
|
73 |
+
|
74 |
+
# # FastAPI endpoint URL
|
75 |
+
# # url = "http://localhost:8084/search/"
|
76 |
+
# url = "https://huggingface.co/search/"
|
77 |
+
|
78 |
+
# # Input fields in Streamlit
|
79 |
+
# id = st.text_input("Enter ID:", value="1")
|
80 |
+
# prompt = st.text_input("Enter your prompt:")
|
81 |
+
# k = st.number_input("Top K results:", min_value=1, max_value=100, value=3)
|
82 |
+
|
83 |
+
# # Trigger the search when the button is clicked
|
84 |
+
# if st.button("Search"):
|
85 |
+
# # Construct the request payload
|
86 |
+
# payload = {
|
87 |
+
# "id": id,
|
88 |
+
# "prompt": prompt,
|
89 |
+
# "k": k
|
90 |
+
# }
|
91 |
+
|
92 |
+
# # Make the POST request
|
93 |
+
# response = requests.post(url, json=payload)
|
94 |
+
|
95 |
+
# # Handle the response
|
96 |
+
# if response.status_code == 200:
|
97 |
+
# results = response.json()
|
98 |
+
# st.write(results)
|
99 |
+
# else:
|
100 |
+
# st.error(f"Error: {response.status_code} - {response.text}")
|