indrasn0wal commited on
Commit
ab2b4ab
·
verified ·
1 Parent(s): 6457fb4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +96 -0
app.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ 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
+ pinecone.init(api_key="2c47d51e-211b-4611-8808-5510e07d1f94", environment="us-east-1")
12
+
13
+ # Assume you have already created and populated an index
14
+ index_name = "zepto"
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)
19
+ return response['data'][0]['embedding']
20
+
21
+ def check_and_correct_spelling(query):
22
+ blob = TextBlob(query)
23
+ corrected_query = str(blob.correct())
24
+ return corrected_query
25
+
26
+ def correct_and_complete_query(text):
27
+ blob = TextBlob(text)
28
+ corrected_text = str(blob.correct())
29
+
30
+ completion_prompt = f"Complete the following query in a way that is related to product search: '{corrected_text}'"
31
+ response = openai.Completion.create(
32
+ model="gpt-3.5-turbo-instruct",
33
+ prompt=completion_prompt,
34
+ max_tokens=100,
35
+ temperature=0.5
36
+ )
37
+
38
+ return response.choices[0].text.strip()
39
+
40
+ def translate_to_english(text):
41
+ if detect(text) != 'en':
42
+ translation_prompt = f"Translate the following text to English:\n\n'{text}'"
43
+ response = openai.Completion.create(
44
+ model="gpt-3.5-turbo-instruct",
45
+ prompt=translation_prompt,
46
+ max_tokens=100,
47
+ temperature=0.5
48
+ )
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)
75
+
76
+ result_strings = []
77
+ for result in search_results['matches']:
78
+ product_name = result['metadata'].get('product_name', 'No name available')
79
+ product_link = result['metadata'].get('product_url', 'No link available')
80
+ score = result['score']
81
+ result_string = f"Product: {product_name}\nLink: {product_link}\nScore: {score}\n"
82
+ result_strings.append(result_string)
83
+
84
+ return "\n".join(result_strings)
85
+
86
+ # Gradio Interface
87
+ interface = gr.Interface(
88
+ fn=search_in_pinecone,
89
+ inputs=gr.Textbox(label="Enter your query"),
90
+ outputs=gr.Textbox(label="Top 5 Similar Products"),
91
+ title="Product Similarity Search",
92
+ description="Enter a query to find the top 5 similar products based on your search."
93
+ )
94
+
95
+ # Launch the interface
96
+ interface.launch()