|
import streamlit as st |
|
from langchain.embeddings.sentence_transformer import SentenceTransformerEmbeddings |
|
from transformers import AutoModel |
|
from pinecone import Pinecone |
|
import os |
|
|
|
|
|
API_KEY = os.environ.get("PINECONE_API_KEY") |
|
|
|
|
|
|
|
pc = Pinecone(api_key=API_KEY) |
|
|
|
|
|
model =SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2") |
|
|
|
def process_and_search(query): |
|
|
|
|
|
|
|
|
|
encoded_query = model.embed_query(query, return_tensors="pt") |
|
|
|
|
|
results = pc.query(INDEX_NAME, encoded_query.cpu().numpy()) |
|
|
|
|
|
processed_results = [] |
|
for result in results: |
|
|
|
answer = result["metadata"]["answer"] |
|
processed_results.append(answer) |
|
|
|
return processed_results |
|
st.title("Pinecone Search App") |
|
|
|
user_query = st.text_area("Enter your question:", height=100) |
|
|
|
if st.button("Search"): |
|
if user_query: |
|
|
|
answers = process_and_search(user_query) |
|
st.write("Search Results:") |
|
for answer in answers: |
|
st.write(f"- {answer}") |
|
else: |
|
st.error("Please enter a question.") |