Spaces:
Sleeping
Sleeping
Upload tool
Browse files- app.py +6 -0
- requirements.txt +4 -0
- tool.py +57 -0
app.py
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import launch_gradio_demo
|
| 2 |
+
from tool import DocumentAnalyzer
|
| 3 |
+
|
| 4 |
+
tool = DocumentAnalyzer()
|
| 5 |
+
|
| 6 |
+
launch_gradio_demo(tool)
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
smolagents
|
| 2 |
+
transformers
|
| 3 |
+
PyPDF2
|
| 4 |
+
requests
|
tool.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Any, Optional
|
| 2 |
+
from smolagents.tools import Tool
|
| 3 |
+
import transformers
|
| 4 |
+
import PyPDF2
|
| 5 |
+
import io
|
| 6 |
+
import requests
|
| 7 |
+
|
| 8 |
+
class DocumentAnalyzer(Tool):
|
| 9 |
+
"""
|
| 10 |
+
A tool that analyzes PDF documents and extracts key information.
|
| 11 |
+
"""
|
| 12 |
+
name = "analyze_document"
|
| 13 |
+
description = "Analyzes a PDF document and extracts key information like summary and sentiment"
|
| 14 |
+
inputs = {'document_url': {'type': 'string', 'description': 'URL to a PDF document'}}
|
| 15 |
+
output_type = "object"
|
| 16 |
+
|
| 17 |
+
def __init__(self):
|
| 18 |
+
super().__init__()
|
| 19 |
+
|
| 20 |
+
def forward(self, document_url: str) -> dict:
|
| 21 |
+
"""
|
| 22 |
+
Analyzes a PDF document and extracts key information.
|
| 23 |
+
|
| 24 |
+
Args:
|
| 25 |
+
document_url (str): URL to a PDF document
|
| 26 |
+
|
| 27 |
+
Returns:
|
| 28 |
+
dict: Contains summary, key points, and sentiment
|
| 29 |
+
"""
|
| 30 |
+
import PyPDF2
|
| 31 |
+
import io
|
| 32 |
+
import requests
|
| 33 |
+
from transformers import pipeline
|
| 34 |
+
|
| 35 |
+
# Download the document
|
| 36 |
+
response = requests.get(document_url)
|
| 37 |
+
pdf_file = io.BytesIO(response.content)
|
| 38 |
+
|
| 39 |
+
# Extract text
|
| 40 |
+
reader = PyPDF2.PdfReader(pdf_file)
|
| 41 |
+
text = ""
|
| 42 |
+
for page in reader.pages:
|
| 43 |
+
text += page.extract_text()
|
| 44 |
+
|
| 45 |
+
# Summarize text
|
| 46 |
+
summarizer = pipeline("summarization", max_length=100)
|
| 47 |
+
summary = summarizer(text[:1024])[0]['summary_text']
|
| 48 |
+
|
| 49 |
+
# Sentiment analysis
|
| 50 |
+
sentiment_analyzer = pipeline("sentiment-analysis")
|
| 51 |
+
sentiment = sentiment_analyzer(text[:512])[0]
|
| 52 |
+
|
| 53 |
+
return {
|
| 54 |
+
"summary": summary,
|
| 55 |
+
"sentiment": sentiment['label'],
|
| 56 |
+
"confidence": sentiment['score']
|
| 57 |
+
}
|