Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from transformers import AutoModel
|
3 |
+
from pinecone import IndexClient
|
4 |
+
import os # For environment variable access
|
5 |
+
|
6 |
+
# Replace with your Space's environment variable name for API key
|
7 |
+
API_KEY = os.environ.get("PINECONE_API_KEY")
|
8 |
+
|
9 |
+
# Connect to Pinecone using the API key retrieved from the Space's environment variable
|
10 |
+
client = IndexClient(api_key=API_KEY)
|
11 |
+
|
12 |
+
# Load pre-trained model (replace with your chosen model)
|
13 |
+
model = AutoModel.from_pretrained("sentence-transformers/all-mpnet-base-v2")
|
14 |
+
|
15 |
+
def process_and_search(query):
|
16 |
+
# Preprocess user input (example: tokenization, normalization)
|
17 |
+
preprocessed_query = preprocess_query(query) # Replace with your implementation
|
18 |
+
|
19 |
+
# Encode the preprocessed query using the pre-trained model
|
20 |
+
encoded_query = model.encode(preprocessed_query, return_tensors="pt")
|
21 |
+
|
22 |
+
# Perform vector search in Pinecone
|
23 |
+
results = client.query(INDEX_NAME, encoded_query.cpu().numpy())
|
24 |
+
|
25 |
+
# Process search results (example: extract answers, format display)
|
26 |
+
processed_results = []
|
27 |
+
for result in results:
|
28 |
+
# Example processing: extract answer from metadata
|
29 |
+
answer = result["metadata"]["answer"] # Adapt based on your data structure
|
30 |
+
processed_results.append(answer)
|
31 |
+
|
32 |
+
return processed_results
|
33 |
+
st.title("Pinecone Search App")
|
34 |
+
|
35 |
+
user_query = st.text_area("Enter your question:", height=100)
|
36 |
+
|
37 |
+
if st.button("Search"):
|
38 |
+
if user_query:
|
39 |
+
# Process, search, and display results (call the process_and_search function)
|
40 |
+
answers = process_and_search(user_query)
|
41 |
+
st.write("Search Results:")
|
42 |
+
for answer in answers:
|
43 |
+
st.write(f"- {answer}")
|
44 |
+
else:
|
45 |
+
st.error("Please enter a question.")
|