Spaces:
Runtime error
Runtime error
File size: 3,113 Bytes
7bbc12d e886d9c 2814bf2 8e4142b 078018e e886d9c ab2b4ab e886d9c ab2b4ab b9e34b3 ab2b4ab e886d9c ab2b4ab e886d9c ab2b4ab e886d9c ab2b4ab 2814bf2 ab2b4ab 2814bf2 ab2b4ab 2814bf2 e886d9c 2814bf2 e886d9c ab2b4ab e886d9c ab2b4ab 2814bf2 ab2b4ab 078018e ab2b4ab e886d9c ab2b4ab e886d9c |
1 2 3 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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
import pandas as pd
import numpy as np
import torch
from textblob import TextBlob
import openai
import gradio as gr
from pinecone import Pinecone
from langdetect import detect
openai.api_key = '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'
pc = Pinecone(api_key="2c47d51e-211b-4611-8808-5510e07d1f94",environment = "us-east-1")
def get_embedding(text, model="text-embedding-ada-002"):
return openai.embeddings.create(input=[text], model=model).data[0].embedding
def check_and_correct_spelling(query):
blob = TextBlob(query)
corrected_query = str(blob.correct())
return corrected_query
def correct_and_complete_query(text):
blob = TextBlob(text)
corrected_text = str(blob.correct())
# Use OpenAI to complete the query
completion_prompt = f"Complete the following query in a way that is related to product search: '{corrected_text}'"
response = openai.completions.create(
model="gpt-3.5-turbo-instruct",
prompt=completion_prompt,
max_tokens=100,
temperature=0.5
)
return response.choices[0].text.strip()
def translate_to_english(text):
if detect(text) != 'en':
translation_prompt = f"Translate the following text to English:\n\n'{text}'"
response = openai.completions.create(
model="gpt-3.5-turbo-instruct",
prompt=translation_prompt,
max_tokens=100,
temperature=0.5
)
return response.choices[0].text.strip()
return text
def is_query_relevant(query, relevant_keywords):
for keyword in relevant_keywords:
if keyword.lower() in query.lower():
return True
return False
def process_query(query):
query = check_and_correct_spelling(query)
query = correct_and_complete_query(query)
query = translate_to_english(query)
# Step 4: Check if the query is relevant
# if not is_query_relevant(query):
# return "The query is not relevant. Please enter a different query."
return query
def search_in_pinecone2(query):
processed_query = process_query(query)
embedding = get_embedding(query)
search_results = index.query(vector=embedding, top_k=5, include_metadata=True)
result_strings = []
for result in search_results['matches']:
product_name = result['metadata'].get('product_name', 'No name available')
product_link = result['metadata'].get('product_url', 'No link available')
score = result['score']
result_string = f"Product: {product_name}\nLink: {product_link}\nScore: {score}\n"
result_strings.append(result_string)
return "\n".join(result_strings)
index = pc.Index('zepto')
interface = gr.Interface(
fn=search_in_pinecone2,
inputs=gr.Textbox(label="Enter your query"),
outputs=gr.Textbox(label="Top 5 Similar Products"),
title="Product Similarity Search",
description="Enter a query to find the top 5 similar products based on your search."
)
# Launch the interface
interface.launch() |