chatbot / app.py
VishnuVardhanBR's picture
Update app.py
f36ba97
import gradio as gr
import subprocess
from finetuned import get_search_query
from scraper_script import scrape_flipkart_products
with open('requirements.txt', 'r') as f:
packages = f.read().splitlines()
for package in packages:
subprocess.run(['pip', 'install', package])
from itertools import islice
from trends import get_trending_brands
with gr.Blocks() as demo:
chatbot = gr.Chatbot().style(height=750)
msg = gr.Textbox(label="Prompt", placeholder="Talk to me here! For example \"I\'m looking for a formal shirt to wear to my interview\" ")
clear = gr.ClearButton([msg, chatbot])
def respond(message, chat_history):
search_query = get_search_query(message).split('\n', 1)[0].strip()
print(search_query)
user_info = {
'name': 'John Doe',
'gender': 'Male',
'age': 34,
'location': 'Hyderabad'
}
try:
popular_brands = get_trending_brands()[:5]
except:
popular_brands
# popular_brands = ['Nike', 'Jockey']
scraped_results = scrape_flipkart_products(search_query, user_info, popular_brands)
if(len(scraped_results) == 0):
bot_message = "I'm sorry, I couldn't find anything."
else:
bot_message = "## Understood! Here are my recommendations:\n\n"
for entry in islice(scraped_results, 3):
bot_message += f"![Product Image]({entry['Image']}) \n"
if(entry['Brand']!='N/A'):
bot_message += f"**Brand:** {entry['Brand']} \n"
bot_message += f"**Description:** {entry['Description']} \n"
bot_message += f"**Price:** {entry['Price']} \n"
bot_message += f"**Deliver By:** {entry['Delivery_Date']} \n"
bot_message += f"**Link:** <a href='{entry['Link']}' target='_blank'>Link</a> \n"
chat_history.append((message, bot_message))
return "", chat_history
msg.submit(respond, [msg, chatbot], [msg, chatbot])
demo.launch()