OpenWheatherMCPapp / src /streamlit_app.py
Rehan12345678's picture
Update src/streamlit_app.py
a83779c verified
Raw History Blame Contribute Delete
1.7 kB
import os
import requests
import streamlit as st
from langchain_groq.chat_models import ChatGroq
from langchain.agents import initialize_agent, Tool
# Set your Groq API key in the environment or Space secrets
# export GROQ_API_KEY="your_groq_api_key"
# Update with your MCP Server URL
MCP_SERVER_URL = "https://rehan12345678-openweathermcpserver.hf.space"
def fetch_weather(city: str):
resp = requests.post(f"{MCP_SERVER_URL}/weather", json={"city": city})
if resp.status_code == 200:
return resp.json()
else:
return {"error": resp.text}
weather_tool = Tool(
name="Weather Tool",
func=lambda city: str(fetch_weather(city)),
description="Get the current weather for a given city (city name as input)."
)
# Initialize Llama 4 Scout via Groq
llm = ChatGroq(
model_name="meta-llama/llama-4-scout-17b-16e-instruct",
temperature=0,
)
agent = initialize_agent(
tools=[weather_tool],
llm=llm,
agent="zero-shot-react-description",
verbose=True,
tool_choice="auto", # allow tool calling automatically
)
# ---------------- Streamlit UI ---------------- #
st.set_page_config(page_title="🌦️ Groq-Powered Weather Chat", layout="centered")
st.title("📡 Weather Assistant MCP Client")
st.write("Ask about the weather in any city")
query = st.text_input("Enter city name or question", "")
if st.button("Get Weather"):
if query.strip():
with st.spinner("Calling Groq..."):
try:
answer = agent.run(query)
st.success(answer)
except Exception as e:
st.error(f"⚠️ Error: {e}")
else:
st.warning("Please enter a question or city name.")