Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,19 @@
|
|
|
|
|
| 1 |
import os
|
| 2 |
import httpx
|
| 3 |
import streamlit as st
|
| 4 |
-
from dotenv import load_dotenv
|
| 5 |
from langchain.prompts import PromptTemplate
|
| 6 |
from langchain.agents import create_react_agent, AgentExecutor
|
| 7 |
from langchain.tools import Tool
|
| 8 |
from langchain_huggingface import HuggingFaceEndpoint
|
| 9 |
import urllib.parse
|
| 10 |
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
HF_TOKEN =
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# Define the tool to fetch weather information
|
| 17 |
def fetch_weather(city: str) -> dict:
|
|
@@ -19,7 +21,6 @@ def fetch_weather(city: str) -> dict:
|
|
| 19 |
city = city.strip() # Ensure no leading/trailing spaces
|
| 20 |
encoded_city = urllib.parse.quote(city)
|
| 21 |
url = f"https://api.openweathermap.org/data/2.5/weather?q={encoded_city}&appid={OPENWEATHER_API_KEY}&units=metric"
|
| 22 |
-
|
| 23 |
try:
|
| 24 |
response = httpx.get(url)
|
| 25 |
response.raise_for_status()
|
|
@@ -32,13 +33,7 @@ def generate_review(w_info : str) -> str:
|
|
| 32 |
"""Generate a review based on the weather information."""
|
| 33 |
if "error" in w_info:
|
| 34 |
return f"Error fetching weather data: {w_info['error']}"
|
| 35 |
-
|
| 36 |
-
# Extract weather details
|
| 37 |
-
#weather = weather_info['weather'][0]['main']
|
| 38 |
-
#temperature = weather_info['main']['temp']
|
| 39 |
-
|
| 40 |
input_text = f"The current weather is {w_info}. Provide a detailed review based on this information."
|
| 41 |
-
|
| 42 |
# Generate the review using the language model
|
| 43 |
review = llm(input_text)
|
| 44 |
return review
|
|
@@ -67,8 +62,7 @@ generate_review_tool = Tool(
|
|
| 67 |
# Define prompts for the agents
|
| 68 |
fetch_weather_prompt = PromptTemplate.from_template("""
|
| 69 |
You are an agent that fetches weather information for a given city.
|
| 70 |
-
City: {input} you can use {tools}
|
| 71 |
-
|
| 72 |
Question: the input question you must answer
|
| 73 |
Thought: you should always think about what to do
|
| 74 |
Action: the action to take, should be one of [{tool_names}]
|
|
@@ -77,16 +71,14 @@ Observation: the result of the action
|
|
| 77 |
... (this Thought/Action/Action Input/Observation can repeat N times)
|
| 78 |
Thought: I now know the final answer
|
| 79 |
Final Answer: the final answer to the original input question
|
| 80 |
-
|
| 81 |
Begin!
|
| 82 |
Question: {input}
|
| 83 |
Thought:
|
| 84 |
{agent_scratchpad}
|
| 85 |
""")
|
| 86 |
|
| 87 |
-
|
| 88 |
generate_review_prompt = PromptTemplate.from_template("""
|
| 89 |
-
You are an expert reviewer and use you can use {tools}
|
| 90 |
Question: the input question you must answer
|
| 91 |
Thought: you should always think about what to do
|
| 92 |
Action: the action to take, should be one of [{tool_names}]
|
|
@@ -95,7 +87,6 @@ Observation: the result of the action
|
|
| 95 |
... (this Thought/Action/Action Input/Observation can repeat N times)
|
| 96 |
Thought: I now know the final answer
|
| 97 |
Final Answer: the final answer to the original input question
|
| 98 |
-
|
| 99 |
Begin!
|
| 100 |
Question: {input}
|
| 101 |
Thought:
|
|
@@ -108,7 +99,6 @@ fetch_weather_agent = create_react_agent(
|
|
| 108 |
tools=[fetch_weather_tool],
|
| 109 |
prompt=fetch_weather_prompt
|
| 110 |
)
|
| 111 |
-
|
| 112 |
generate_review_agent = create_react_agent(
|
| 113 |
llm=llm,
|
| 114 |
tools=[generate_review_tool],
|
|
@@ -118,34 +108,21 @@ generate_review_agent = create_react_agent(
|
|
| 118 |
fetch_weather_agent_executor = AgentExecutor(agent=fetch_weather_agent, tools=[fetch_weather_tool], verbose=True,handle_parsing_errors=True )
|
| 119 |
generate_review_agent_executor = AgentExecutor(agent=generate_review_agent, tools=[generate_review_tool], verbose=True,)
|
| 120 |
|
| 121 |
-
st.title("Weather Information and Review")
|
| 122 |
-
|
| 123 |
# Streamlit UI
|
| 124 |
-
city = st.text_input("Enter the name of a city:")
|
| 125 |
-
|
| 126 |
if st.button("Get Weather Information and Review"):
|
| 127 |
with st.spinner("Processing..."):
|
| 128 |
try:
|
| 129 |
# Fetch weather information
|
| 130 |
-
weather_info = fetch_weather_agent_executor.invoke({
|
| 131 |
-
'input': city
|
| 132 |
-
})
|
| 133 |
-
# st.write(weather_info['output'])
|
| 134 |
w_info=weather_info['output']
|
| 135 |
-
# Handle case where the response is not as expected
|
| 136 |
if 'error' in weather_info:
|
| 137 |
st.error(f"Error fetching weather data: {weather_info['error']}")
|
| 138 |
else:
|
| 139 |
# Generate review based on the weather information
|
| 140 |
-
review = generate_review_agent_executor.invoke({
|
| 141 |
-
'input': w_info
|
| 142 |
-
})
|
| 143 |
-
|
| 144 |
st.subheader("Weather Information")
|
| 145 |
st.write(weather_info['output'])
|
| 146 |
-
|
| 147 |
st.subheader("AI Generated Weather Review")
|
| 148 |
st.write(review['output'])
|
| 149 |
-
|
| 150 |
except Exception as e:
|
| 151 |
-
st.error(f"Error: {e}")
|
|
|
|
| 1 |
+
# Running fine :)
|
| 2 |
import os
|
| 3 |
import httpx
|
| 4 |
import streamlit as st
|
|
|
|
| 5 |
from langchain.prompts import PromptTemplate
|
| 6 |
from langchain.agents import create_react_agent, AgentExecutor
|
| 7 |
from langchain.tools import Tool
|
| 8 |
from langchain_huggingface import HuggingFaceEndpoint
|
| 9 |
import urllib.parse
|
| 10 |
|
| 11 |
+
st.title("City Weather Information with AI Review")
|
| 12 |
+
OPENWEATHER_API_KEY = st.sidebar.text_input("Enter Weather API Key", type="password")
|
| 13 |
+
st.sidebar.write("Check out this [Weather API](https://home.openweathermap.org/api_keys) to generate API key")
|
| 14 |
+
HF_TOKEN = st.sidebar.text_input("Enter Hugging Face API Key", type="password")
|
| 15 |
+
st.sidebar.write("Check out this [Hugging Face Token](https://huggingface.co/settings/tokens) to generate token")
|
| 16 |
+
city = st.text_input("Enter the name of a city:")
|
| 17 |
|
| 18 |
# Define the tool to fetch weather information
|
| 19 |
def fetch_weather(city: str) -> dict:
|
|
|
|
| 21 |
city = city.strip() # Ensure no leading/trailing spaces
|
| 22 |
encoded_city = urllib.parse.quote(city)
|
| 23 |
url = f"https://api.openweathermap.org/data/2.5/weather?q={encoded_city}&appid={OPENWEATHER_API_KEY}&units=metric"
|
|
|
|
| 24 |
try:
|
| 25 |
response = httpx.get(url)
|
| 26 |
response.raise_for_status()
|
|
|
|
| 33 |
"""Generate a review based on the weather information."""
|
| 34 |
if "error" in w_info:
|
| 35 |
return f"Error fetching weather data: {w_info['error']}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
input_text = f"The current weather is {w_info}. Provide a detailed review based on this information."
|
|
|
|
| 37 |
# Generate the review using the language model
|
| 38 |
review = llm(input_text)
|
| 39 |
return review
|
|
|
|
| 62 |
# Define prompts for the agents
|
| 63 |
fetch_weather_prompt = PromptTemplate.from_template("""
|
| 64 |
You are an agent that fetches weather information for a given city.
|
| 65 |
+
City: {input} you can use {tools}
|
|
|
|
| 66 |
Question: the input question you must answer
|
| 67 |
Thought: you should always think about what to do
|
| 68 |
Action: the action to take, should be one of [{tool_names}]
|
|
|
|
| 71 |
... (this Thought/Action/Action Input/Observation can repeat N times)
|
| 72 |
Thought: I now know the final answer
|
| 73 |
Final Answer: the final answer to the original input question
|
|
|
|
| 74 |
Begin!
|
| 75 |
Question: {input}
|
| 76 |
Thought:
|
| 77 |
{agent_scratchpad}
|
| 78 |
""")
|
| 79 |
|
|
|
|
| 80 |
generate_review_prompt = PromptTemplate.from_template("""
|
| 81 |
+
You are an expert reviewer and use you can use {tools} for weather review
|
| 82 |
Question: the input question you must answer
|
| 83 |
Thought: you should always think about what to do
|
| 84 |
Action: the action to take, should be one of [{tool_names}]
|
|
|
|
| 87 |
... (this Thought/Action/Action Input/Observation can repeat N times)
|
| 88 |
Thought: I now know the final answer
|
| 89 |
Final Answer: the final answer to the original input question
|
|
|
|
| 90 |
Begin!
|
| 91 |
Question: {input}
|
| 92 |
Thought:
|
|
|
|
| 99 |
tools=[fetch_weather_tool],
|
| 100 |
prompt=fetch_weather_prompt
|
| 101 |
)
|
|
|
|
| 102 |
generate_review_agent = create_react_agent(
|
| 103 |
llm=llm,
|
| 104 |
tools=[generate_review_tool],
|
|
|
|
| 108 |
fetch_weather_agent_executor = AgentExecutor(agent=fetch_weather_agent, tools=[fetch_weather_tool], verbose=True,handle_parsing_errors=True )
|
| 109 |
generate_review_agent_executor = AgentExecutor(agent=generate_review_agent, tools=[generate_review_tool], verbose=True,)
|
| 110 |
|
|
|
|
|
|
|
| 111 |
# Streamlit UI
|
|
|
|
|
|
|
| 112 |
if st.button("Get Weather Information and Review"):
|
| 113 |
with st.spinner("Processing..."):
|
| 114 |
try:
|
| 115 |
# Fetch weather information
|
| 116 |
+
weather_info = fetch_weather_agent_executor.invoke({'input': city})
|
|
|
|
|
|
|
|
|
|
| 117 |
w_info=weather_info['output']
|
|
|
|
| 118 |
if 'error' in weather_info:
|
| 119 |
st.error(f"Error fetching weather data: {weather_info['error']}")
|
| 120 |
else:
|
| 121 |
# Generate review based on the weather information
|
| 122 |
+
review = generate_review_agent_executor.invoke({'input': w_info})
|
|
|
|
|
|
|
|
|
|
| 123 |
st.subheader("Weather Information")
|
| 124 |
st.write(weather_info['output'])
|
|
|
|
| 125 |
st.subheader("AI Generated Weather Review")
|
| 126 |
st.write(review['output'])
|
|
|
|
| 127 |
except Exception as e:
|
| 128 |
+
st.error(f"Error: {e}")
|