pratikshahp commited on
Commit
ce6df47
·
verified ·
1 Parent(s): 3dfe59d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -0
app.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
51
+ Review:
52
+ """
53
+
54
+ # Create and configure the graph
55
+ builder = MessageGraph()
56
+
57
+ # Add nodes
58
+ builder.add_node("fetch_weather", fetch_weather_node)
59
+ builder.add_node("generate_review", generate_review_node)
60
+ builder.set_entry_point("fetch_weather")
61
+
62
+ # Define transitions
63
+ builder.add_edge("fetch_weather", "generate_review")
64
+ builder.set_finish_point("generate_review")
65
+
66
+ # Compile the graph
67
+ graph = builder.compile()
68
+
69
+ # Streamlit app
70
+ st.title("City Weather Information with AI Review")
71
+
72
+ city = st.text_input("Enter the name of a city:")
73
+
74
+ if st.button("Get Weather Information and Review"):
75
+ if city:
76
+ with st.spinner("Processing..."):
77
+ try:
78
+ # Prepare the input for the graph
79
+ weather_info = graph.invoke(HumanMessage(content=city))
80
+ st.write(weather_info[1].content)
81
+ # Generate the review using the refined prompt
82
+ review_input = review_prompt_template.format(weather_info=weather_info)
83
+ review = graph.invoke(HumanMessage(content=review_input))
84
+
85
+ st.subheader("AI Generated Weather Review")
86
+ st.write(review[2].content)
87
+ st.subheader("Mermaid Graph")
88
+ st.write("Check out this [mermaid link](https://mermaid.live/) to display a graph with following data")
89
+ st.write(graph.get_graph().draw_mermaid())
90
+ graph.get_graph().print_ascii()
91
+ except Exception as e:
92
+ st.error(f"Error generating weather review: {e}")
93
+ else:
94
+ st.warning("Please enter a city name.")