Spaces:
Configuration error
Configuration error
Upload 2 files
Browse files- app (1).py +117 -0
- requiements.txt +5 -0
app (1).py
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
# Get API keys from the user
|
| 12 |
+
st.title("City Weather Information with AI Review")
|
| 13 |
+
OPENWEATHER_API_KEY = st.sidebar.text_input("Enter Weather API Key", type="password")
|
| 14 |
+
st.sidebar.write("Check out this [Weather API](https://home.openweathermap.org/api_keys) to generate API key")
|
| 15 |
+
HF_TOKEN = st.sidebar.text_input("Enter Hugging Face API Key", type="password")
|
| 16 |
+
st.sidebar.write("Check out this [Hugging Face Token](https://huggingface.co/settings/tokens) to generate token")
|
| 17 |
+
city = st.text_input("Enter the name of a city:")
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
# Define a tool to fetch weather information and generate a review
|
| 21 |
+
def fetch_and_review_weather(city: str) -> str:
|
| 22 |
+
"""Fetch the weather information for a given city and generate a review."""
|
| 23 |
+
city = city.strip() # Ensure no leading/trailing spaces
|
| 24 |
+
|
| 25 |
+
# Encode the city name for the URL
|
| 26 |
+
encoded_city = urllib.parse.quote(city)
|
| 27 |
+
url = f"https://api.openweathermap.org/data/2.5/weather?q={encoded_city}&appid={OPENWEATHER_API_KEY}&units=metric"
|
| 28 |
+
|
| 29 |
+
try:
|
| 30 |
+
response = httpx.get(url)
|
| 31 |
+
response.raise_for_status()
|
| 32 |
+
weather_data = response.json()
|
| 33 |
+
|
| 34 |
+
# Extract weather details
|
| 35 |
+
weather = weather_data['weather'][0]['main']
|
| 36 |
+
temperature = weather_data['main']['temp']
|
| 37 |
+
|
| 38 |
+
# Generate the review
|
| 39 |
+
input_text = f"The current weather in {city} is {weather} with a temperature of {temperature}°C. As an expert in weather forecast analysis, please provide an appropriate weather review."
|
| 40 |
+
input_text = ''.join(c for c in input_text if c.isprintable())
|
| 41 |
+
|
| 42 |
+
# Generate the review using the language model
|
| 43 |
+
review = llm(input_text)
|
| 44 |
+
return review
|
| 45 |
+
except Exception as e:
|
| 46 |
+
return f"Error: {e}"
|
| 47 |
+
|
| 48 |
+
# Initialize the HuggingFace inference endpoint
|
| 49 |
+
llm = HuggingFaceEndpoint(
|
| 50 |
+
repo_id="mistralai/Mistral-7B-Instruct-v0.3",
|
| 51 |
+
huggingfacehub_api_token=HF_TOKEN.strip(),
|
| 52 |
+
temperature=0.7,
|
| 53 |
+
max_new_tokens=200
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
# Define the tool for the agent
|
| 57 |
+
tools_for_agent = [
|
| 58 |
+
Tool(
|
| 59 |
+
name="fetch_and_review_weather",
|
| 60 |
+
func=fetch_and_review_weather,
|
| 61 |
+
description="Fetch the weather information for a given city and generate a review"
|
| 62 |
+
)
|
| 63 |
+
]
|
| 64 |
+
|
| 65 |
+
# Define a prompt template for the agent
|
| 66 |
+
template = """
|
| 67 |
+
Answer the following questions as best you can. You have access to the following tools:
|
| 68 |
+
|
| 69 |
+
{tools}
|
| 70 |
+
|
| 71 |
+
Use the following format:
|
| 72 |
+
|
| 73 |
+
Question: the input question you must answer
|
| 74 |
+
Thought: you should always think about what to do
|
| 75 |
+
Action: the action to take, should be one of [{tool_names}]
|
| 76 |
+
Action Input: the input to the action
|
| 77 |
+
Observation: the result of the action
|
| 78 |
+
... (this Thought/Action/Action Input/Observation can repeat N times)
|
| 79 |
+
Thought: I now know the final answer
|
| 80 |
+
Final Answer: the final answer to the original input question
|
| 81 |
+
|
| 82 |
+
Begin!
|
| 83 |
+
|
| 84 |
+
Question: {input}
|
| 85 |
+
Thought:
|
| 86 |
+
{agent_scratchpad}
|
| 87 |
+
"""
|
| 88 |
+
|
| 89 |
+
prompt = PromptTemplate.from_template(template=template).partial(
|
| 90 |
+
tools="\n".join([f"{t.name}: {t.description}" for t in tools_for_agent]),
|
| 91 |
+
tool_names=", ".join([t.name for t in tools_for_agent]),
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
# Initialize the agent
|
| 95 |
+
react_prompt = prompt
|
| 96 |
+
agent = create_react_agent(
|
| 97 |
+
llm=llm,
|
| 98 |
+
tools=tools_for_agent,
|
| 99 |
+
prompt=react_prompt
|
| 100 |
+
)
|
| 101 |
+
agent_executor = AgentExecutor(agent=agent, tools=tools_for_agent, verbose=True)
|
| 102 |
+
|
| 103 |
+
# Streamlit UI for fetching and reviewing weather
|
| 104 |
+
|
| 105 |
+
if st.button("Get Weather Information and Review"):
|
| 106 |
+
with st.spinner("Processing..."):
|
| 107 |
+
try:
|
| 108 |
+
# Directly call the fetch_and_review_weather function through the agent
|
| 109 |
+
review = agent_executor.invoke({
|
| 110 |
+
'input': f"Generate weather review for {city}",
|
| 111 |
+
'agent_scratchpad': ''
|
| 112 |
+
}, handle_parsing_errors=True)
|
| 113 |
+
|
| 114 |
+
st.subheader("AI Generated Weather Review")
|
| 115 |
+
st.write(review['output'])
|
| 116 |
+
except Exception as e:
|
| 117 |
+
st.error(f"Error generating weather review: {e}")
|
requiements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
langchain
|
| 2 |
+
httpx
|
| 3 |
+
streamlit
|
| 4 |
+
python-dotenv
|
| 5 |
+
langchain-huggingface
|