Spaces:
Sleeping
Sleeping
pratikshahp
commited on
Commit
•
dadd618
1
Parent(s):
82bdadd
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import httpx
|
2 |
+
import streamlit as st
|
3 |
+
from langchain.prompts import PromptTemplate
|
4 |
+
from langchain_huggingface import HuggingFaceEndpoint
|
5 |
+
from langchain_core.messages import BaseMessage, HumanMessage
|
6 |
+
from langgraph.graph import MessageGraph, END
|
7 |
+
from typing import Sequence
|
8 |
+
|
9 |
+
# streamlit app
|
10 |
+
st.title("City Weather Information with AI Review")
|
11 |
+
OPENWEATHER_API_KEY = st.sidebar.text_input("Enter Weather API Key", type="password")
|
12 |
+
st.sidebar.write("Check out this [Weather API](https://home.openweathermap.org/api_keys) to generate API key")
|
13 |
+
HF_TOKEN = st.sidebar.text_input("Enter Hugging Face API Key", type="password")
|
14 |
+
st.sidebar.write("Check out this [Hugging Face Token](https://huggingface.co/settings/tokens) to generate token")
|
15 |
+
city = st.text_input("Enter the name of a city:")
|
16 |
+
|
17 |
+
# Initialize the HuggingFace inference endpoint
|
18 |
+
llm = HuggingFaceEndpoint(
|
19 |
+
repo_id="mistralai/Mistral-7B-Instruct-v0.3",
|
20 |
+
huggingfacehub_api_token=HF_TOKEN.strip(),
|
21 |
+
temperature=0.7,
|
22 |
+
max_new_tokens=100
|
23 |
+
)
|
24 |
+
|
25 |
+
# Define nodes
|
26 |
+
def fetch_weather_node(state: Sequence[BaseMessage]) -> str:
|
27 |
+
city = state[0].content.strip()
|
28 |
+
url = f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={OPENWEATHER_API_KEY}&units=metric"
|
29 |
+
|
30 |
+
try:
|
31 |
+
response = httpx.get(url)
|
32 |
+
response.raise_for_status()
|
33 |
+
weather_data = response.json()
|
34 |
+
weather = weather_data['weather'][0]['main']
|
35 |
+
temperature = weather_data['main']['temp']
|
36 |
+
return f"The current weather in {city} is {weather} with a temperature of {temperature}°C."
|
37 |
+
except Exception as e:
|
38 |
+
return f"Error: {e}"
|
39 |
+
|
40 |
+
def generate_review_node(state: Sequence[BaseMessage]) -> str:
|
41 |
+
input_text = state[0].content
|
42 |
+
response = llm(input_text)
|
43 |
+
return response
|
44 |
+
|
45 |
+
# Define the prompt template for generating weather reviews
|
46 |
+
review_prompt_template = """
|
47 |
+
You are an expert weather analyst. Based on the provided weather information, generate a detailed and insightful review.
|
48 |
+
Weather Information: {weather_info[1]}
|
49 |
+
Your review should include an analysis of the weather conditions.
|
50 |
+
Review:
|
51 |
+
"""
|
52 |
+
|
53 |
+
# Create and configure the graph
|
54 |
+
builder = MessageGraph()
|
55 |
+
|
56 |
+
# Add nodes
|
57 |
+
builder.add_node("fetch_weather", fetch_weather_node)
|
58 |
+
builder.add_node("generate_review", generate_review_node)
|
59 |
+
builder.set_entry_point("fetch_weather")
|
60 |
+
|
61 |
+
# Define transitions
|
62 |
+
builder.add_edge("fetch_weather", "generate_review")
|
63 |
+
builder.set_finish_point("generate_review")
|
64 |
+
|
65 |
+
# Compile the graph
|
66 |
+
graph = builder.compile()
|
67 |
+
|
68 |
+
# Streamlit app
|
69 |
+
if st.button("Get Weather Information and Review"):
|
70 |
+
if city:
|
71 |
+
with st.spinner("Processing..."):
|
72 |
+
try:
|
73 |
+
# Prepare the input for the graph
|
74 |
+
weather_info = graph.invoke(HumanMessage(content=city))
|
75 |
+
st.write(weather_info[1].content)
|
76 |
+
# Generate the review using the refined prompt
|
77 |
+
review_input = review_prompt_template.format(weather_info=weather_info)
|
78 |
+
review = graph.invoke(HumanMessage(content=review_input))
|
79 |
+
|
80 |
+
st.subheader("AI Generated Weather Review")
|
81 |
+
st.write(review[2].content)
|
82 |
+
st.subheader("Mermaid Graph")
|
83 |
+
st.write("Check out this [mermaid link](https://mermaid.live/) to display a graph with following data")
|
84 |
+
#st.write(graph.get_graph().draw_mermaid())
|
85 |
+
mermaid_code = graph.get_graph().draw_mermaid()
|
86 |
+
st.markdown(f"```mermaid\n{mermaid_code}\n```", unsafe_allow_html=True)
|
87 |
+
|
88 |
+
except Exception as e:
|
89 |
+
st.error(f"Error generating weather review: {e}")
|
90 |
+
else:
|
91 |
+
st.warning("Please enter a city name.")
|