Spaces:
Runtime error
Runtime error
import asyncio | |
import nest_asyncio | |
from langchain_community.agent_toolkits.playwright.toolkit import PlayWrightBrowserToolkit | |
from langchain_community.tools.playwright.utils import create_async_playwright_browser | |
from langchain_openai import ChatOpenAI | |
from langchain.prompts import PromptTemplate | |
from langchain.chains import LLMChain | |
import gradio as gr | |
# Allow nested async calls. | |
nest_asyncio.apply() | |
async def extract_reviews(url): | |
# Create an asynchronous browser instance using Playwright. | |
async_browser = create_async_playwright_browser() | |
# Get the browser toolkit which provides utility functions. | |
toolkit = PlayWrightBrowserToolkit.from_browser(async_browser=async_browser) | |
tools = toolkit.get_tools() | |
# Create a dictionary for accessing tools by their name. | |
tools_by_name = {tool.name: tool for tool in tools} | |
navigate_tool = tools_by_name["navigate_browser"] | |
get_elements_tool = tools_by_name["get_elements"] | |
# Navigate to the Amazon product reviews URL. | |
await navigate_tool.arun({"url": url}) | |
# Extract reviews from the webpage using the provided selector. | |
elements = await get_elements_tool.arun({"selector": ".review", "attributes": ["innerText"]}) | |
# Close the browser after extraction. | |
await async_browser.close() | |
return elements | |
async def summarize_reviews(url, openai_api_key): | |
reviews = await extract_reviews(url) | |
# Define the template for the prompt. | |
prompt_template = """ | |
From the reviews delimited by ``` | |
Provide a detailed summary of the reviews to find the pros and cons of the product. | |
Simplify the words used in the reviews and provide more information about the product, | |
including its features, functionality, and performance. Also, mention the brand name and | |
give an overview of what the product is about. Additionally, provide an overall rating | |
score from 1 (very poor) to 5 (best) based on the reviews. | |
``` | |
{reviews} | |
``` | |
""" | |
# Initialize the prompt template. | |
prompt = PromptTemplate(template=prompt_template, input_variables=["reviews"]) | |
# Initialize the OpenAI Chat model. | |
llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo", openai_api_key=openai_api_key) | |
# Create an extraction chain using the schema and the Chat model. | |
chain = LLMChain(llm=llm, prompt=prompt) | |
# Get the summarized results. | |
summary = chain.run(reviews=reviews) | |
return summary | |
async def main(url, openai_api_key): | |
summary = await summarize_reviews(url, openai_api_key) | |
return summary | |
# Gradio Interface | |
def gradio_interface(openai_api_key, url): | |
summary = asyncio.run(main(url, openai_api_key)) | |
return summary | |
iface = gr.Interface( | |
fn=gradio_interface, | |
inputs=[ | |
gr.Textbox(lines=1, placeholder="Enter OpenAI API Key Here..."), | |
gr.Textbox(lines=2, placeholder="Enter Website URL Here...") | |
], | |
outputs="text", | |
title="Product Review Summarizer", | |
description="Input the OpenAI API key and product URL to extract and summarize reviews.", | |
live=False, | |
allow_flagging="never" | |
) | |
if __name__ == "__main__": | |
iface.launch() | |