Spaces:
Runtime error
Runtime error
Add application file
Browse files
app.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import AutoModelForCausalLM, pipeline
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import pandas as pd
|
| 6 |
+
import pytesseract
|
| 7 |
+
|
| 8 |
+
# Load models
|
| 9 |
+
text_model = AutoModelForCausalLM.from_pretrained("microsoft/Florence-2-large", trust_remote_code=True)
|
| 10 |
+
tts_pipeline = pipeline("text-to-speech", model="parler-tts/parler-tts-large-v1")
|
| 11 |
+
|
| 12 |
+
# Function to process PDF files
|
| 13 |
+
def process_pdf(pdf):
|
| 14 |
+
text = ""
|
| 15 |
+
# Assuming each page in the PDF is processed into text
|
| 16 |
+
for page in pdf.pages:
|
| 17 |
+
text += pytesseract.image_to_string(page)
|
| 18 |
+
return text
|
| 19 |
+
|
| 20 |
+
# Function to process CSV files
|
| 21 |
+
def process_csv(csv):
|
| 22 |
+
df = pd.read_csv(csv)
|
| 23 |
+
return df.to_string()
|
| 24 |
+
|
| 25 |
+
# Function to process images
|
| 26 |
+
def process_image(image):
|
| 27 |
+
return pytesseract.image_to_string(image)
|
| 28 |
+
|
| 29 |
+
# Main function that handles all file types
|
| 30 |
+
def handle_files(file):
|
| 31 |
+
if file.name.endswith('.pdf'):
|
| 32 |
+
text = process_pdf(file)
|
| 33 |
+
elif file.name.endswith('.csv'):
|
| 34 |
+
text = process_csv(file)
|
| 35 |
+
else:
|
| 36 |
+
image = Image.open(file)
|
| 37 |
+
text = process_image(image)
|
| 38 |
+
|
| 39 |
+
# Generate audio from the text
|
| 40 |
+
audio = tts_pipeline(text)
|
| 41 |
+
|
| 42 |
+
return text, audio["audio"]
|
| 43 |
+
|
| 44 |
+
# Gradio interface
|
| 45 |
+
demo = gr.Interface(
|
| 46 |
+
fn=handle_files,
|
| 47 |
+
inputs=gr.File(type=["pdf", "csv", "image"]),
|
| 48 |
+
outputs=[gr.Textbox(label="Extracted Text"), gr.Audio(label="Generated Audio")],
|
| 49 |
+
title="AuditBidden - Public Procurement Auditor"
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
if __name__ == "__main__":
|
| 53 |
+
demo.launch()
|