Kunal commited on
Commit ·
0848f94
1
Parent(s): b74efa4
updated app.py with agent and added requirements.txt
Browse files- .gitignore +1 -0
- app.py +67 -4
- requirements.txt +4 -0
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
.env
|
app.py
CHANGED
|
@@ -1,7 +1,70 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from smolagents import LiteLLMModel, CodeAgent, tool
|
| 3 |
+
import os
|
| 4 |
+
from pypdf import PdfReader
|
| 5 |
|
| 6 |
+
#Initialize the Model
|
| 7 |
+
model = LiteLLMModel(
|
| 8 |
+
model_name="gemini/gemini-1.5-flash",
|
| 9 |
+
api_key=os.environ.get("GEMINI_API_KEY")
|
| 10 |
+
)
|
| 11 |
|
| 12 |
+
#tool to extract text from PDF
|
| 13 |
+
@tool
|
| 14 |
+
def process_pdf(file_path: str) -> str:
|
| 15 |
+
"""
|
| 16 |
+
Extract text from a PDF file.
|
| 17 |
+
Args:
|
| 18 |
+
file_path (str): Path to the PDF file.
|
| 19 |
+
Returns:
|
| 20 |
+
str: Extracted text from the PDF.
|
| 21 |
+
"""
|
| 22 |
+
reader = PdfReader(file_path)
|
| 23 |
+
return "\n".join([page.extract_text() for page in reader.pages])
|
| 24 |
+
|
| 25 |
+
#tool to analysis the pdf for answering question
|
| 26 |
+
@tool
|
| 27 |
+
def chat_with_pdf(query: str, pdf_text: str, chat_history: list) -> str:
|
| 28 |
+
"""
|
| 29 |
+
Answer questions about the PDF content.
|
| 30 |
+
Args:
|
| 31 |
+
query (str): The question to answer.
|
| 32 |
+
pdf_text (str): The text extracted from the PDF.
|
| 33 |
+
chat_history (list): Previous chat history.
|
| 34 |
+
Returns:
|
| 35 |
+
str: Answer to the question.
|
| 36 |
+
"""
|
| 37 |
+
pass
|
| 38 |
+
|
| 39 |
+
#Agent to handle the chat with PDF
|
| 40 |
+
agent = CodeAgent(
|
| 41 |
+
model=model,
|
| 42 |
+
tools=[process_pdf, chat_with_pdf],
|
| 43 |
+
verbose=True
|
| 44 |
+
)
|
| 45 |
+
#Gradio Interface
|
| 46 |
+
def process_pdf_ui(file):
|
| 47 |
+
pdf_text = process_pdf(file.name)
|
| 48 |
+
return pdf_text
|
| 49 |
+
|
| 50 |
+
def chat_ui(query, history, pdf_text):
|
| 51 |
+
response = agent.chat(
|
| 52 |
+
f"PDF Context: {pdf_text}\nUser Query: {query}\nChat History: {history}"
|
| 53 |
+
)
|
| 54 |
+
return response, history + [(query, response)]
|
| 55 |
+
|
| 56 |
+
with gr.Blocks() as demo:
|
| 57 |
+
gr.Markdown("# PDF Chatbot")
|
| 58 |
+
|
| 59 |
+
with gr.Row():
|
| 60 |
+
pdf_input = gr.File(label="Upload PDF", file_types=[".pdf"])
|
| 61 |
+
pdf_text = gr.Textbox(visible=False)
|
| 62 |
+
|
| 63 |
+
chatbot = gr.Chatbot()
|
| 64 |
+
msg = gr.Textbox(placeholder="Ask a question about the PDF")
|
| 65 |
+
|
| 66 |
+
pdf_input.upload(process_pdf_ui, inputs=pdf_input, outputs=pdf_text)
|
| 67 |
+
msg.submit(chat_ui, inputs=[msg, chatbot, pdf_text], outputs=[chatbot, msg])
|
| 68 |
+
|
| 69 |
+
if __name__ == "__main__":
|
| 70 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# requirements.txt
|
| 2 |
+
smolagents[litellm]
|
| 3 |
+
pypdf
|
| 4 |
+
gradio
|