import gradio as gr import os from langchain.agents import initialize_agent from langchain.llms import OpenAI from gradio_tools import BaseTool from langchain.memory import ConversationBufferMemory # Set up OpenAI API Key if not os.getenv("OPENAI_API_KEY"): os.environ["OPENAI_API_KEY"] = "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" # Simulated method to fetch a review from Google Maps (You'll replace this with an actual implementation) def fetch_gmaps_review(location): # Simulated review review = "The location was outstanding with beautiful views and amazing food. Service could be better though." return review class GMapsReviewSummarizationTool(BaseTool): def __init__(self) -> None: super().__init__(name="GMapsReviewSummarizer", description="A tool that fetches a Google Maps review based on a location and then summarizes it.", src="your_gradio_app_url_or_id_here") def create_job(self, query: str) -> str: review = fetch_gmaps_review(query) return review def postprocess(self, output: str) -> str: llm = OpenAI(temperature=0.5) response = llm.query(f"Summarize the following Google Maps review: '{output}'") return response def main(): llm = OpenAI(temperature=0.5) memory = ConversationBufferMemory(memory_key="review_history") tools = [GMapsReviewSummarizationTool().langchain] agent = initialize_agent(tools, llm, memory=memory, agent="conversational-review-summarizer", verbose=True) def gr_interface(location_input): return agent.run(input=f"Fetch and summarize a review for the location: {location_input}") interface = gr.Interface(fn=gr_interface, inputs="textbox", outputs="textbox", live=True, title="GMaps Review Summarizer") interface.launch() if __name__ == "__main__": main()