Spaces:
Sleeping
Sleeping
Commit
·
fd59124
1
Parent(s):
04b1d6c
[bug]: modified pdf not showing
Browse files- app.py +16 -8
- src/streamlit_app.py +105 -0
app.py
CHANGED
@@ -24,15 +24,23 @@ with tab1:
|
|
24 |
st.header("PDF Agent")
|
25 |
uploaded_pdf = st.file_uploader("Upload a PDF", type=["pdf"])
|
26 |
question = st.text_input("Ask a question about the PDF:")
|
|
|
|
|
27 |
if uploaded_pdf and question:
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
with tab2:
|
38 |
st.header("Weather Agent")
|
|
|
24 |
st.header("PDF Agent")
|
25 |
uploaded_pdf = st.file_uploader("Upload a PDF", type=["pdf"])
|
26 |
question = st.text_input("Ask a question about the PDF:")
|
27 |
+
if uploaded_pdf:
|
28 |
+
st.info(f"PDF uploaded: {uploaded_pdf.name}, size: {uploaded_pdf.size} bytes")
|
29 |
if uploaded_pdf and question:
|
30 |
+
try:
|
31 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
|
32 |
+
tmp_file.write(uploaded_pdf.read())
|
33 |
+
tmp_path = tmp_file.name
|
34 |
+
st.info(f"Saved PDF to temp file: {tmp_path}")
|
35 |
+
pdf_agent = PDFAgent(pdf_path=tmp_path)
|
36 |
+
with st.spinner("Processing..."):
|
37 |
+
answer = pdf_agent.ask(question)
|
38 |
+
st.success("Answer:")
|
39 |
+
st.write(answer)
|
40 |
+
except Exception as e:
|
41 |
+
st.error(f"Error processing PDF: {e}")
|
42 |
+
import traceback
|
43 |
+
st.text(traceback.format_exc())
|
44 |
|
45 |
with tab2:
|
46 |
st.header("Weather Agent")
|
src/streamlit_app.py
ADDED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import asyncio
|
2 |
+
import streamlit as st
|
3 |
+
import tempfile
|
4 |
+
from agents.pdf_agent import PDFAgent
|
5 |
+
from agents.weather_agent import WeatherAgent
|
6 |
+
|
7 |
+
# Ensure an event loop exists for async libraries (fix for Google Generative AI Embeddings)
|
8 |
+
try:
|
9 |
+
asyncio.get_event_loop()
|
10 |
+
except RuntimeError:
|
11 |
+
asyncio.set_event_loop(asyncio.new_event_loop())
|
12 |
+
|
13 |
+
st.set_page_config(page_title="LangGraph Agents Demo", layout="wide")
|
14 |
+
st.title("LangGraph Agents Demo")
|
15 |
+
|
16 |
+
tab1, tab2, tab3 = st.tabs(["PDF Agent", "Weather Agent", "Multi-Agent QA"])
|
17 |
+
|
18 |
+
with tab1:
|
19 |
+
st.header("PDF Agent")
|
20 |
+
uploaded_pdf = st.file_uploader("Upload a PDF", type=["pdf"])
|
21 |
+
question = st.text_input("Ask a question about the PDF:")
|
22 |
+
if uploaded_pdf:
|
23 |
+
st.info(f"PDF uploaded: {uploaded_pdf.name}, size: {uploaded_pdf.size} bytes")
|
24 |
+
if uploaded_pdf and question:
|
25 |
+
try:
|
26 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
|
27 |
+
tmp_file.write(uploaded_pdf.read())
|
28 |
+
tmp_path = tmp_file.name
|
29 |
+
st.info(f"Saved PDF to temp file: {tmp_path}")
|
30 |
+
pdf_agent = PDFAgent(pdf_path=tmp_path)
|
31 |
+
with st.spinner("Processing..."):
|
32 |
+
answer = pdf_agent.ask(question)
|
33 |
+
st.success("Answer:")
|
34 |
+
st.write(answer)
|
35 |
+
except Exception as e:
|
36 |
+
st.error(f"Error processing PDF: {e}")
|
37 |
+
import traceback
|
38 |
+
st.text(traceback.format_exc())
|
39 |
+
|
40 |
+
with tab2:
|
41 |
+
st.header("Weather Agent")
|
42 |
+
location = st.text_input("Enter a location for weather info: e.g. Mumbai")
|
43 |
+
if location:
|
44 |
+
weather_agent = WeatherAgent()
|
45 |
+
with st.spinner("Fetching weather..."):
|
46 |
+
try:
|
47 |
+
result = weather_agent.ask(location)
|
48 |
+
st.success("Weather Info:")
|
49 |
+
st.write(result) # This might be None or a dict
|
50 |
+
# Try to extract the answer if it's a dict or object
|
51 |
+
# if isinstance(result, dict):
|
52 |
+
# # Try common keys
|
53 |
+
# if "output" in result:
|
54 |
+
# st.write(result["output"])
|
55 |
+
# elif "result" in result:
|
56 |
+
# st.write(result["result"])
|
57 |
+
# else:
|
58 |
+
# st.write(str(result))
|
59 |
+
# elif hasattr(result, "content"):
|
60 |
+
# st.write(result.content)
|
61 |
+
# elif result is not None:
|
62 |
+
# st.write(str(result))
|
63 |
+
except Exception as e:
|
64 |
+
st.error(f"Error: {e}")
|
65 |
+
|
66 |
+
with tab3:
|
67 |
+
st.header("Multi-Agent QA (PDF + Weather)")
|
68 |
+
user_input = st.text_area("Ask multiple questions (e.g. 'What organizations has Sharath worked for and tell me the weather in Mumbai'):")
|
69 |
+
uploaded_pdf = st.file_uploader("Upload a PDF for PDF Agent (optional)", type=["pdf"], key="multi_pdf")
|
70 |
+
if st.button("Ask Multi-Agent"):
|
71 |
+
from nodes.node import split_questions, classify_question
|
72 |
+
from langchain_core.messages import HumanMessage
|
73 |
+
import tempfile
|
74 |
+
messages = []
|
75 |
+
# If PDF uploaded, save and use it
|
76 |
+
pdf_path = None
|
77 |
+
if uploaded_pdf:
|
78 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
|
79 |
+
tmp_file.write(uploaded_pdf.read())
|
80 |
+
pdf_path = tmp_file.name
|
81 |
+
# Split and process each question
|
82 |
+
questions = split_questions(user_input)
|
83 |
+
for question in questions:
|
84 |
+
agent_name = classify_question(question)
|
85 |
+
if agent_name == "pdf_agent":
|
86 |
+
if pdf_path:
|
87 |
+
pdf_agent = PDFAgent(pdf_path=pdf_path)
|
88 |
+
else:
|
89 |
+
pdf_agent = PDFAgent(pdf_path="Sharath_OnePage.pdf")
|
90 |
+
result = pdf_agent.agent.invoke({"input": question})
|
91 |
+
if isinstance(result, dict):
|
92 |
+
text_result = result.get("output") or result.get("text") or str(result)
|
93 |
+
else:
|
94 |
+
text_result = str(result)
|
95 |
+
messages.append(("PDF Agent", text_result))
|
96 |
+
else:
|
97 |
+
weather_agent = WeatherAgent()
|
98 |
+
import re
|
99 |
+
match = re.search(r"weather in ([\w\s,]+)", question, re.IGNORECASE)
|
100 |
+
location = match.group(1).strip() if match else question
|
101 |
+
result = weather_agent.ask(location)
|
102 |
+
messages.append(("Weather Agent", str(result)))
|
103 |
+
st.subheader("Results:")
|
104 |
+
for agent, answer in messages:
|
105 |
+
st.markdown(f"**{agent}:** {answer}")
|