Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import streamlit as st
|
3 |
+
from crewai import Agent, Task, Crew
|
4 |
+
from crewai_tools import LlamaIndexTool
|
5 |
+
from langchain_groq import ChatGroq
|
6 |
+
from llama_index.core import SimpleDirectoryReader, VectorStoreIndex
|
7 |
+
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
8 |
+
import tempfile
|
9 |
+
import requests
|
10 |
+
|
11 |
+
# --- Streamlit UI Header ---
|
12 |
+
st.title("Document Q&A Assistant with CrewAI")
|
13 |
+
st.write("Upload a document, provide a link, or ask questions dynamically!")
|
14 |
+
|
15 |
+
# --- Key Configuration from Secrets ---
|
16 |
+
try:
|
17 |
+
GROQ_API_KEY = st.secrets["GROQ_API_KEY"]
|
18 |
+
TAVILY_API_KEY = st.secrets["TAVILY_API_KEY"]
|
19 |
+
except KeyError as e:
|
20 |
+
st.error(f"Missing API key in secrets: {e}. Please add it to your environment.")
|
21 |
+
st.stop()
|
22 |
+
|
23 |
+
# Check if all API keys are available
|
24 |
+
if not GROQ_API_KEY or not TAVILY_API_KEY:
|
25 |
+
st.error("One or more required API keys are missing. Please check your configuration.")
|
26 |
+
st.stop()
|
27 |
+
|
28 |
+
# Function to download PDF from URL
|
29 |
+
def download_pdf_from_url(url, save_path):
|
30 |
+
response = requests.get(url)
|
31 |
+
if response.status_code == 200:
|
32 |
+
with open(save_path, 'wb') as f:
|
33 |
+
f.write(response.content)
|
34 |
+
return save_path
|
35 |
+
else:
|
36 |
+
st.error("Failed to download PDF from the provided URL.")
|
37 |
+
return None
|
38 |
+
|
39 |
+
# --- User Inputs for File or Link ---
|
40 |
+
document_source = st.radio("Choose input method:", ("Upload a PDF", "Provide PDF URL"))
|
41 |
+
|
42 |
+
pdf_path = None
|
43 |
+
if document_source == "Upload a PDF":
|
44 |
+
uploaded_file = st.file_uploader("Upload a PDF file", type=['pdf'])
|
45 |
+
if uploaded_file:
|
46 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_file:
|
47 |
+
temp_file.write(uploaded_file.getvalue())
|
48 |
+
pdf_path = temp_file.name
|
49 |
+
st.success("File uploaded successfully!")
|
50 |
+
else:
|
51 |
+
pdf_url = st.text_input("Enter PDF URL")
|
52 |
+
if st.button("Download PDF") and pdf_url:
|
53 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_file:
|
54 |
+
saved_path = download_pdf_from_url(pdf_url, temp_file.name)
|
55 |
+
if saved_path:
|
56 |
+
pdf_path = saved_path
|
57 |
+
|
58 |
+
# --- LLM Configuration ---
|
59 |
+
llm = ChatGroq(groq_api_key=GROQ_API_KEY, model="groq/llama-3.3-70b-versatile")
|
60 |
+
|
61 |
+
# Function to create Query Engine
|
62 |
+
def create_query_engine(pdf_path, llm):
|
63 |
+
reader = SimpleDirectoryReader(input_files=[pdf_path])
|
64 |
+
docs = reader.load_data()
|
65 |
+
embed_model = HuggingFaceEmbedding(model_name="BAAI/bge-small-en-v1.5")
|
66 |
+
index = VectorStoreIndex.from_documents(docs, embed_model=embed_model)
|
67 |
+
return index.as_query_engine(similarity_top_k=5)
|
68 |
+
|
69 |
+
# --- Streamlit Question Workflow ---
|
70 |
+
if pdf_path:
|
71 |
+
st.success("PDF loaded successfully!")
|
72 |
+
query_engine = create_query_engine(pdf_path, llm)
|
73 |
+
query_tool = LlamaIndexTool.from_query_engine(
|
74 |
+
query_engine,
|
75 |
+
name="Document Query Tool",
|
76 |
+
description="Tool to analyze and retrieve information from the uploaded document."
|
77 |
+
)
|
78 |
+
|
79 |
+
# Define Agents and Tasks
|
80 |
+
researcher = Agent(
|
81 |
+
role="Document Analyst",
|
82 |
+
goal="Analyze documents and answer questions",
|
83 |
+
backstory="Expert at retrieving insights from documents.",
|
84 |
+
verbose=True,
|
85 |
+
allow_delegation=False,
|
86 |
+
tools=[query_tool],
|
87 |
+
llm=llm,
|
88 |
+
)
|
89 |
+
|
90 |
+
task = Task(
|
91 |
+
description="Answer user queries based on the uploaded document.",
|
92 |
+
expected_output="Clear and concise answers to user questions.",
|
93 |
+
agent=researcher,
|
94 |
+
)
|
95 |
+
|
96 |
+
crew = Crew(agents=[researcher], tasks=[task], verbose=True)
|
97 |
+
|
98 |
+
st.subheader("Ask a Question")
|
99 |
+
user_question = st.text_input("Enter your question")
|
100 |
+
|
101 |
+
if st.button("Get Answer"):
|
102 |
+
with st.spinner("Processing your request..."):
|
103 |
+
result = crew.kickoff(inputs={"question": user_question})
|
104 |
+
st.success("Here is the answer:")
|
105 |
+
st.write(result)
|
106 |
+
else:
|
107 |
+
st.warning("Please upload a PDF or provide a valid URL to continue.")
|
108 |
+
|
109 |
+
# --- Clean Up ---
|
110 |
+
if pdf_path and os.path.exists(pdf_path):
|
111 |
+
os.remove(pdf_path)
|