manuth commited on
Commit
c2e7967
·
1 Parent(s): 16fbcfe

Add application file

Browse files
Files changed (3) hide show
  1. .env +1 -0
  2. app.py +94 -0
  3. requirements.txt +0 -0
.env ADDED
@@ -0,0 +1 @@
 
 
1
+ OPENAI_API_KEY = 'sk-proj-aBuDB2BxH30A7ityocWiT3BlbkFJS0mf2ammDemVDNcQ97gp'
app.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import asyncio
2
+ import nest_asyncio
3
+ from langchain_community.agent_toolkits.playwright.toolkit import PlayWrightBrowserToolkit
4
+ from langchain_community.tools.playwright.utils import create_async_playwright_browser
5
+ from langchain_openai import ChatOpenAI
6
+ from langchain.prompts import PromptTemplate
7
+ from langchain.chains import LLMChain
8
+ import os
9
+ from dotenv import load_dotenv, find_dotenv
10
+ import gradio as gr
11
+
12
+ # Load environment variables from the .env file.
13
+ load_dotenv(find_dotenv())
14
+
15
+ # Allow nested async calls.
16
+ nest_asyncio.apply()
17
+
18
+ async def extract_reviews(url):
19
+ # Create an asynchronous browser instance using Playwright.
20
+ async_browser = create_async_playwright_browser()
21
+
22
+ # Get the browser toolkit which provides utility functions.
23
+ toolkit = PlayWrightBrowserToolkit.from_browser(async_browser=async_browser)
24
+ tools = toolkit.get_tools()
25
+
26
+ # Create a dictionary for accessing tools by their name.
27
+ tools_by_name = {tool.name: tool for tool in tools}
28
+ navigate_tool = tools_by_name["navigate_browser"]
29
+ get_elements_tool = tools_by_name["get_elements"]
30
+
31
+ # Navigate to the Amazon product reviews URL.
32
+ await navigate_tool.arun({"url": url})
33
+
34
+ # Extract reviews from the webpage using the provided selector.
35
+ elements = await get_elements_tool.arun({"selector": ".review", "attributes": ["innerText"]})
36
+
37
+ # Close the browser after extraction.
38
+ await async_browser.close()
39
+
40
+ return elements
41
+
42
+ async def summarize_reviews(url):
43
+ reviews = await extract_reviews(url)
44
+
45
+ # Define the template for the prompt.
46
+ prompt_template = """
47
+ From the reviews delimited by ```
48
+ Provide a detailed summary of the reviews to find the pros and cons of the product.
49
+ Simplify the words used in the reviews and provide more information about the product,
50
+ including its features, functionality, and performance. Also, mention the brand name and
51
+ give an overview of what the product is about. Additionally, provide an overall rating
52
+ score from 1 (very poor) to 5 (best) based on the reviews.
53
+
54
+ ```
55
+ {reviews}
56
+ ```
57
+ """
58
+
59
+ # Initialize the prompt template.
60
+ prompt = PromptTemplate(template=prompt_template, input_variables=["reviews"])
61
+
62
+ # Initialize the OpenAI Chat model.
63
+ llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo", openai_api_key=os.getenv("OPENAI_API_KEY"))
64
+
65
+ # Create an extraction chain using the schema and the Chat model.
66
+ chain = LLMChain(llm=llm, prompt=prompt)
67
+
68
+ # Get the summarized results.
69
+ summary = chain.run(reviews=reviews)
70
+
71
+ return summary
72
+
73
+ async def main(url):
74
+ summary = await summarize_reviews(url)
75
+ return summary
76
+
77
+ # Gradio Interface
78
+ def gradio_interface(url):
79
+ summary = asyncio.run(main(url))
80
+ return summary
81
+
82
+
83
+ iface = gr.Interface(
84
+ fn=gradio_interface,
85
+ inputs=gr.Textbox(lines=2, placeholder="Enter Website URL Here..."),
86
+ outputs="text",
87
+ title="Product Review Summarizer",
88
+ description="Input the product URL to extract and summarize reviews.",
89
+ live=True,
90
+ allow_flagging="never"
91
+ )
92
+
93
+ if __name__ == "__main__":
94
+ iface.launch(debug=True)
requirements.txt ADDED
Binary file (3.97 kB). View file