manuth commited on
Commit
33a3e0e
·
1 Parent(s): c2e7967

using openai key as the input

Browse files
Files changed (2) hide show
  1. .env +0 -1
  2. app.py +13 -16
.env DELETED
@@ -1 +0,0 @@
1
- OPENAI_API_KEY = 'sk-proj-aBuDB2BxH30A7ityocWiT3BlbkFJS0mf2ammDemVDNcQ97gp'
 
 
app.py CHANGED
@@ -5,13 +5,8 @@ from langchain_community.tools.playwright.utils import create_async_playwright_b
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
 
@@ -39,7 +34,7 @@ async def extract_reviews(url):
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.
@@ -60,7 +55,7 @@ async def summarize_reviews(url):
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)
@@ -70,25 +65,27 @@ async def summarize_reviews(url):
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)
 
5
  from langchain_openai import ChatOpenAI
6
  from langchain.prompts import PromptTemplate
7
  from langchain.chains import LLMChain
 
 
8
  import gradio as gr
9
 
 
 
 
10
  # Allow nested async calls.
11
  nest_asyncio.apply()
12
 
 
34
 
35
  return elements
36
 
37
+ async def summarize_reviews(url, openai_api_key):
38
  reviews = await extract_reviews(url)
39
 
40
  # Define the template for the prompt.
 
55
  prompt = PromptTemplate(template=prompt_template, input_variables=["reviews"])
56
 
57
  # Initialize the OpenAI Chat model.
58
+ llm = ChatOpenAI(temperature=0, model="gpt-3.5-turbo", openai_api_key=openai_api_key)
59
 
60
  # Create an extraction chain using the schema and the Chat model.
61
  chain = LLMChain(llm=llm, prompt=prompt)
 
65
 
66
  return summary
67
 
68
+ async def main(url, openai_api_key):
69
+ summary = await summarize_reviews(url, openai_api_key)
70
  return summary
71
 
72
  # Gradio Interface
73
+ def gradio_interface(openai_api_key, url):
74
+ summary = asyncio.run(main(url, openai_api_key))
75
  return summary
76
 
 
77
  iface = gr.Interface(
78
  fn=gradio_interface,
79
+ inputs=[
80
+ gr.Textbox(lines=1, placeholder="Enter OpenAI API Key Here..."),
81
+ gr.Textbox(lines=2, placeholder="Enter Website URL Here...")
82
+ ],
83
  outputs="text",
84
  title="Product Review Summarizer",
85
+ description="Input the OpenAI API key and product URL to extract and summarize reviews.",
86
+ live=False,
87
  allow_flagging="never"
88
  )
89
 
90
  if __name__ == "__main__":
91
+ iface.launch(debug=True, share=True)