Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -3,16 +3,15 @@ import openai
|
|
3 |
import pinecone
|
4 |
from textblob import TextBlob
|
5 |
from langdetect import detect
|
6 |
-
|
7 |
# Initialize OpenAI API
|
8 |
openai.api_key = "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" # Replace with your OpenAI API key
|
9 |
|
10 |
# Initialize Pinecone
|
11 |
-
|
12 |
|
13 |
# Assume you have already created and populated an index
|
14 |
-
|
15 |
-
index = pinecone.Index(index_name)
|
16 |
|
17 |
def get_embedding(text, model="text-embedding-ada-002"):
|
18 |
response = openai.Embedding.create(input=[text], model=model)
|
@@ -49,26 +48,15 @@ def translate_to_english(text):
|
|
49 |
return response.choices[0].text.strip()
|
50 |
return text
|
51 |
|
52 |
-
def is_query_relevant(query, relevant_keywords=["product", "buy", "purchase"]):
|
53 |
-
for keyword in relevant_keywords:
|
54 |
-
if keyword.lower() in query.lower():
|
55 |
-
return True
|
56 |
-
return False
|
57 |
-
|
58 |
def process_query(query):
|
59 |
query = check_and_correct_spelling(query)
|
60 |
query = correct_and_complete_query(query)
|
61 |
query = translate_to_english(query)
|
62 |
|
63 |
-
if not is_query_relevant(query):
|
64 |
-
return "The query is not relevant. Please enter a different query."
|
65 |
-
|
66 |
return query
|
67 |
|
68 |
def search_in_pinecone(query):
|
69 |
processed_query = process_query(query)
|
70 |
-
if processed_query.startswith("The query is not relevant"):
|
71 |
-
return processed_query
|
72 |
|
73 |
embedding = get_embedding(processed_query)
|
74 |
search_results = index.query(vector=embedding, top_k=5, include_metadata=True)
|
|
|
3 |
import pinecone
|
4 |
from textblob import TextBlob
|
5 |
from langdetect import detect
|
6 |
+
from pinecone import Pinecone
|
7 |
# Initialize OpenAI API
|
8 |
openai.api_key = "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" # Replace with your OpenAI API key
|
9 |
|
10 |
# Initialize Pinecone
|
11 |
+
pc = Pinecone(api_key="2c47d51e-211b-4611-8808-5510e07d1f94", environment="us-east-1")
|
12 |
|
13 |
# Assume you have already created and populated an index
|
14 |
+
index = pc.Index('zepto')
|
|
|
15 |
|
16 |
def get_embedding(text, model="text-embedding-ada-002"):
|
17 |
response = openai.Embedding.create(input=[text], model=model)
|
|
|
48 |
return response.choices[0].text.strip()
|
49 |
return text
|
50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
def process_query(query):
|
52 |
query = check_and_correct_spelling(query)
|
53 |
query = correct_and_complete_query(query)
|
54 |
query = translate_to_english(query)
|
55 |
|
|
|
|
|
|
|
56 |
return query
|
57 |
|
58 |
def search_in_pinecone(query):
|
59 |
processed_query = process_query(query)
|
|
|
|
|
60 |
|
61 |
embedding = get_embedding(processed_query)
|
62 |
search_results = index.query(vector=embedding, top_k=5, include_metadata=True)
|