Dy commited on
Commit
e7e3fea
1 Parent(s): 298dbbb

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +231 -0
app.py ADDED
@@ -0,0 +1,231 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from bs4 import BeautifulSoup
3
+ import gradio as gr
4
+ import openai
5
+ import requests
6
+ from langchain import OpenAI, ConversationChain, LLMChain, PromptTemplate
7
+ from langchain.memory import ConversationBufferWindowMemory
8
+ from langchain.chains import LLMChain
9
+ from langchain.agents import load_tools, initialize_agent
10
+ from langchain.chat_models import ChatOpenAI
11
+ from langchain.output_parsers import CommaSeparatedListOutputParser
12
+ from langchain.prompts import PromptTemplate, ChatPromptTemplate, HumanMessagePromptTemplate
13
+ from langchain.llms import OpenAI
14
+ from collections import Counter
15
+ import pandas as pd
16
+
17
+ OPENAI_API_KEY = os.environ['OPENAI_API_KEY']
18
+ GOOGLE_MAPS_API = os.environ['GOOGLE_MAPS_API']
19
+
20
+ #### TAB 1 ####
21
+
22
+ def get_location_data(search_term, location):
23
+ # First, we get the latitude and longitude coordinates of the location
24
+ url = "https://maps.googleapis.com/maps/api/geocode/json"
25
+ params = {
26
+ "address": location,
27
+ "key": GOOGLE_MAPS_API
28
+ }
29
+ response = requests.get(url, params=params)
30
+ location_data = response.json()["results"][0]["geometry"]["location"]
31
+
32
+ # Next, we use the Places API nearbysearch endpoint to find places matching the search term
33
+ url = "https://maps.googleapis.com/maps/api/place/nearbysearch/json"
34
+ params = {
35
+ "location": f"{location_data['lat']},{location_data['lng']}",
36
+ "radius": "10000", # 10km radius
37
+ #"type": search_term,
38
+ "keyword" : search_term,
39
+ "key": GOOGLE_MAPS_API
40
+ }
41
+ response = requests.get(url, params=params)
42
+ results = response.json()["results"]
43
+
44
+ # We only want the first 5 results
45
+ results = results[:5]
46
+
47
+ # For each result, we get the place details to retrieve the description and top reviews
48
+ locations = []
49
+ for result in results:
50
+ place_id = result["place_id"]
51
+ url = "https://maps.googleapis.com/maps/api/place/details/json"
52
+ params = {
53
+ "place_id": place_id,
54
+ "fields": "name,formatted_address,formatted_phone_number,rating,review",
55
+ "key": GOOGLE_MAPS_API
56
+ }
57
+ response = requests.get(url, params=params)
58
+ place_details = response.json()["result"]
59
+
60
+ # Create a dictionary representing the location and add it to the list
61
+ location_dict = {
62
+ "name": place_details["name"],
63
+ "address": place_details["formatted_address"],
64
+ #"phone_number": place_details.get("formatted_phone_number", "N/A"),
65
+ #"rating": place_details.get("rating", "N/A"),
66
+ "reviews": []
67
+ }
68
+
69
+ # Add the top 3 reviews to the dictionary
70
+ reviews = place_details.get("reviews", [])
71
+ for review in reviews[:3]:
72
+ review_dict = {
73
+ #"author": review["author_name"],
74
+ #"rating": review["rating"],
75
+ "text": review["text"],
76
+ #"time": review["relative_time_description"]
77
+ }
78
+ location_dict["reviews"].append(review_dict)
79
+
80
+ locations.append(location_dict)
81
+
82
+ return locations
83
+
84
+ # Define the function to be used in the Gradio app
85
+ def find_competitors(product, location):
86
+ locations = get_location_data(product, location)
87
+ if len(locations) == 0:
88
+ return f"No competitors found for {product} in {location}."
89
+
90
+ output_str = f"Top competitors for {product} in {location}:"
91
+ for i, loc in enumerate(locations):
92
+ output_str += f"\n{i+1}. {loc['name']}"
93
+ output_str += f"\nAddress: {loc['address']}"
94
+ #output_str += f"\nPhone number: {loc['phone_number']}"
95
+ #output_str += f"\nRating: {loc['rating']}"
96
+ output_str += f"\nTop 3 reviews:"
97
+ for review in loc['reviews']:
98
+ output_str += f"\n- {review['text']}"
99
+ #output_str += f"\n Author: {review['author']}"
100
+ #output_str += f"\n Rating: {review['rating']}"
101
+ #output_str += f"\n Time: {review['time']}"
102
+
103
+ output_str2 = f"Top competitors for {product} in {location}:"
104
+ for i, loc in enumerate(locations):
105
+ output_str2 += f"\n{i+1}. {loc['name']}"
106
+ output_str2 += f"\nAddress: {loc['address']}"
107
+
108
+ #return output_str
109
+
110
+ prompt_input = '''
111
+ You are an expert management consultant that rivals the best of Mckinsey, Bain, BCG.
112
+ The client wants to sell {} in {}.
113
+ {}
114
+ Provide an analysis of the following:
115
+ - From the competition and reviews about its products and come up with creative insights to recommend the client execute as part of a differentiating business strategy.
116
+ - From there, think step by step, explain 5 strategies in bullet points of a creative and effective business plan.
117
+ - Suggest a location for the client and explain the rationale of this locatioin.
118
+ - Let us think step by step.
119
+ '''.format(product, location, output_str)
120
+
121
+ template = '''
122
+ {history}
123
+ {human_input}
124
+ '''
125
+ prompt = PromptTemplate(
126
+ input_variables=["history", "human_input"],
127
+ template=template
128
+ )
129
+
130
+ chatgpt_chain = LLMChain(
131
+ llm=ChatOpenAI(model="gpt-3.5-turbo", temperature=0.5,openai_api_key=OPENAI_API_KEY),
132
+ prompt=prompt,
133
+ verbose=True,
134
+ memory=ConversationBufferWindowMemory(k=10),
135
+ )
136
+
137
+ output = output_str2 + "\n\n" + chatgpt_chain.predict(human_input=prompt_input)
138
+
139
+ return(output)
140
+
141
+ # Create the Gradio app interface
142
+ inputs = [
143
+ gr.inputs.Textbox(label="Product to research"),
144
+ gr.inputs.Textbox(label="Location")
145
+ ]
146
+
147
+
148
+ output = gr.outputs.Textbox(label="AI Analysis")
149
+
150
+ iface1 = gr.Interface(fn=find_competitors, inputs=inputs, outputs=output, title="Market Research AI",
151
+ description="Input a product and a location. The AI analyst will help you research nearby competitors, formulate a business plan to differentiate you from your competitors, and recommend a strategic location for your business.")
152
+
153
+ #### TAB 2 ####
154
+
155
+ template2 = '''
156
+ {history}
157
+ {human_input}
158
+ '''
159
+ prompt2 = PromptTemplate(
160
+ input_variables=["history", "human_input"],
161
+ template=template2
162
+ )
163
+
164
+ chatgpt_chain = LLMChain(
165
+ llm=ChatOpenAI(model="gpt-3.5-turbo", temperature=0.5,openai_api_key=OPENAI_API_KEY),
166
+ prompt=prompt2,
167
+ verbose=True,
168
+ memory=ConversationBufferWindowMemory(k=10),
169
+ )
170
+
171
+ # Scrape the URL
172
+ def scrape(url):
173
+ response = requests.get(url)
174
+ soup = BeautifulSoup(response.text, "html.parser")
175
+
176
+ # Remove script and style elements
177
+ for script in soup(["script", "style"]):
178
+ script.extract()
179
+
180
+ return soup.get_text()
181
+
182
+ # Extract keywords
183
+ def extract_keywords(prompt_input, num_keywords):
184
+
185
+ output= chatgpt_chain.predict(human_input=prompt_input)
186
+ output_parser = CommaSeparatedListOutputParser()
187
+ ret_list = output_parser.parse(output)
188
+
189
+ return ret_list
190
+
191
+ # Define the function to be used in Gradio
192
+ def keywords_from_url(url, num_keywords):
193
+ url_text = scrape(url)
194
+ prompt_input2 = '''
195
+ You are an expert SEO optimized, consultant and manager.
196
+
197
+ Here is the text from a website:
198
+
199
+ {}
200
+
201
+ From the text above, extract {} SEO keyphrase that are highly valueble in terms of SEO purpose.
202
+
203
+ Your response should be a list of comma separated values, eg: `foo, bar, baz
204
+ '''.format(url_text, num_keywords)
205
+
206
+ keywords = extract_keywords(prompt_input2, num_keywords)
207
+
208
+ df = pd.DataFrame(keywords, columns=["Keyword"])
209
+ df.index.name = "Rank"
210
+ df.index += 1
211
+ df.to_csv('keywords.csv')
212
+
213
+ return "keywords.csv"
214
+
215
+
216
+
217
+ iface2 = gr.Interface(
218
+ fn=keywords_from_url,
219
+ inputs=[gr.inputs.Textbox(label="URL"), gr.inputs.Slider(minimum=1, maximum=50, step=1, default=10, label="Number of SEO Keywords")],
220
+ outputs=gr.outputs.File(label="Download CSV File"),
221
+ title="SEO Keyword Extractor",
222
+ description="Enter a URL and the number of keywords you want to extract from that page. The output will be a CSV file containing the SEO keywords."
223
+ )
224
+
225
+
226
+
227
+ #tab1 = gr.Tab("AI Market Research", inputs=iface1.inputs, outputs=iface1.outputs)
228
+ #tab2 = gr.Tab("SEO Keyword Extractor", inputs=iface2.inputs, outputs=iface2.outputs)
229
+
230
+ demo = gr.TabbedInterface([iface2, iface1], ["SEO Keyword Extractor", "AI Market Researcher"])
231
+ demo.launch()