DrishtiSharma's picture
Update app.py
8e0a54a verified
# to-do : provide option to download the generated report in .txt or .pdf format
import streamlit as st
from langchain_community.tools import TavilySearchResults
from langchain_groq import ChatGroq
from langgraph.graph import StateGraph, END
from typing import TypedDict, List
# --- Setup API Keys ---
import os
TAVILY_API_KEY = os.getenv("TAVILY_API_KEY")
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
# --- Instantiate LLM ---
llm = ChatGroq(model="Llama3-8b-8192")
# --- Define State ---
class GraphState(TypedDict):
question: str
web_search_results: str
generate: str
documents: List[str]
# --- Define Nodes ---
def web_search_results(state: GraphState):
""" Perform web search based on question """
st.write("**Web Search in Progress...**")
question = state.get("question", "")
documents = state.get("documents", [])
web_search_tool = TavilySearchResults(k=3)
docs = web_search_tool.invoke({"query": question})
documents.extend(docs)
return {"documents": documents, "question": question}
def generation(state: GraphState):
""" Generate response based on search results """
st.write("**Generating Report...**")
question = state["question"]
documents = state['documents']
prompt = f"""Based on the SEARCH RESULTS and QUESTION provided below, prepare a detailed report:
SEARCH RESULTS:
{documents}
QUESTION:
{question}
The Report should include the following information:
1. Introduction
2. Key Competitors of the retail company mentioned in the QUESTION
3. Store foot fall in the area for each of the key competitors
4. Peak Customer hours for each of the competitors
5. Actionable items to enhance business strategy of the retail company in QUESTION
6. Conclusion
7. References: URLs corresponding to each competitor
Please provide the report in MARKDOWN format.
"""
generation = llm.invoke(prompt).content
return {"generate": generation, "question": question, "documents": documents}
# --- Create Workflow ---
graph = StateGraph(GraphState)
graph.add_node("websearch", web_search_results)
graph.add_node("generation", generation)
graph.set_entry_point("websearch")
graph.add_edge("websearch", "generation")
graph.add_edge("generation", END)
app = graph.compile()
# --- Streamlit UI ---
st.title("Clothing Retail Competitor Insights")
st.write("""
This app generates a professional report analyzing competitors of a clothing retailer in a specific location, including:
- Key competitors
- Store footfall
- Peak customer hours
- Actionable insights
""")
# --- User Inputs ---
retailer_name = st.text_input("Enter Retailer Name (e.g., H&M)", "H&M")
location = st.text_input("Enter Location (e.g., Noida Sector-18, Uttar Pradesh)", "Noida Sector-18, Uttar Pradesh")
if st.button("Generate Report"):
question = f"Potential clothing store retailers who are competitors to {retailer_name} in {location} along with their Store foot fall, customer peak hours."
inputs = {"question": question}
response = app.invoke(inputs)
st.markdown(response['generate'])
# --- Workflow Visualization ---
st.subheader("Workflow Visualization")
st.image(app.get_graph().draw_mermaid_png(), caption="Workflow Graph")