antfraia commited on
Commit
ae62a59
·
1 Parent(s): b0b9bed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -44
app.py CHANGED
@@ -1,46 +1,53 @@
1
  import gradio as gr
2
- import requests
3
-
4
- API_KEY = "AIzaSyAr__CqumxiYScFXx2QG191SYvwZeWj2H8"
5
-
6
- def fetch_reviews(place_id: str, reviews_limit: int, ranking: str):
7
- # Build the request URL for Google Maps Places API
8
- base_url = "https://maps.googleapis.com/maps/api/place/details/json"
9
- params = {
10
- "fields": "name,rating,formatted_phone_number,reviews",
11
- "place_id": place_id,
12
- "key": API_KEY
13
- }
14
- response = requests.get(base_url, params=params)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
- if response.status_code == 200:
17
- data = response.json()
18
- reviews = data.get("result", {}).get("reviews", [])[:reviews_limit]
19
-
20
- # Format the reviews for display
21
- formatted_reviews = []
22
- for review in reviews:
23
- review_text = f"Author: {review['author_name']}\nRating: {review['rating']}\n{review.get('text', '')}\n\n"
24
- formatted_reviews.append(review_text)
25
-
26
- return "\n".join(formatted_reviews)
27
-
28
- else:
29
- return "Error fetching reviews. Please try again."
30
-
31
- # Define Gradio Interface
32
- interface = gr.Interface(
33
- fn=fetch_reviews,
34
- inputs=[
35
- gr.components.Textbox(label="Google Maps Place ID", placeholder="ChIJN1t_tDeuEmsRUsoyG83frY4"),
36
- gr.components.Slider(minimum=1, maximum=5, label="Number of Reviews", value=3),
37
- gr.components.Dropdown(choices=["most_relevant", "newest"], label="Reviews Ranking Type", value="most_relevant")
38
- ],
39
- outputs=gr.components.Textbox(),
40
- live=True,
41
- title="Google Maps Reviews Fetcher",
42
- description="Fetch reviews from Google Maps based on your criteria."
43
- )
44
-
45
- # If you're testing locally:
46
- interface.launch()
 
1
  import gradio as gr
2
+ import os
3
+ from langchain.agents import initialize_agent
4
+ from langchain.llms import OpenAI
5
+ from gradio_tools import BaseTool
6
+ from langchain.memory import ConversationBufferMemory
7
+
8
+ # Set up OpenAI API Key
9
+ if not os.getenv("OPENAI_API_KEY"):
10
+ os.environ["OPENAI_API_KEY"] = "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"
11
+
12
+ # Simulated method to fetch a review from Google Maps (You'll replace this with an actual implementation)
13
+ def fetch_gmaps_review(location):
14
+ # Simulated review
15
+ review = "The location was outstanding with beautiful views and amazing food. Service could be better though."
16
+ return review
17
+
18
+ class GMapsReviewSummarizationTool(BaseTool):
19
+
20
+ def __init__(self) -> None:
21
+ super().__init__(name="GMapsReviewSummarizer",
22
+ description="A tool that fetches a Google Maps review based on a location and then summarizes it.",
23
+ src="your_gradio_app_url_or_id_here")
24
+
25
+ def create_job(self, query: str) -> str:
26
+ review = fetch_gmaps_review(query)
27
+ return review
28
+
29
+ def postprocess(self, output: str) -> str:
30
+ llm = OpenAI(temperature=0.5)
31
+ response = llm.query(f"Summarize the following Google Maps review: '{output}'")
32
+ return response
33
+
34
+ def main():
35
+ llm = OpenAI(temperature=0.5)
36
+ memory = ConversationBufferMemory(memory_key="review_history")
37
+ tools = [GMapsReviewSummarizationTool().langchain]
38
+
39
+ agent = initialize_agent(tools, llm, memory=memory, agent="conversational-review-summarizer", verbose=True)
40
+
41
+ def gr_interface(location_input):
42
+ return agent.run(input=f"Fetch and summarize a review for the location: {location_input}")
43
+
44
+ interface = gr.Interface(fn=gr_interface,
45
+ inputs="textbox",
46
+ outputs="textbox",
47
+ live=True,
48
+ title="GMaps Review Summarizer")
49
 
50
+ interface.launch()
51
+
52
+ if __name__ == "__main__":
53
+ main()