Spaces:
Sleeping
Sleeping
Umar Majeed
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,17 @@
|
|
| 1 |
-
import
|
| 2 |
-
import os
|
| 3 |
import requests
|
| 4 |
import pdfplumber
|
| 5 |
import torch
|
| 6 |
-
import ffmpeg
|
| 7 |
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
|
| 8 |
-
import streamlit as st
|
| 9 |
from reportlab.lib.pagesizes import letter
|
| 10 |
-
from reportlab.pdfgen import canvas
|
| 11 |
-
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
| 12 |
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
|
|
|
|
|
|
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
# Define paths for temporary files
|
| 19 |
-
temp_audio_folder = "/tmp/audios/"
|
| 20 |
-
temp_pdf_path = "/tmp/uploaded_pdf.pdf"
|
| 21 |
-
temp_output_pdf_path = "/tmp/response_output.pdf"
|
| 22 |
-
|
| 23 |
-
# Ensure temporary directories exist
|
| 24 |
-
os.makedirs(temp_audio_folder, exist_ok=True)
|
| 25 |
|
| 26 |
# Setup models
|
| 27 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
|
@@ -40,7 +30,6 @@ whisper_pipe = pipeline(
|
|
| 40 |
device=device
|
| 41 |
)
|
| 42 |
|
| 43 |
-
# Granite model URL and headers
|
| 44 |
granite_url = "https://us-south.ml.cloud.ibm.com/ml/v1/text/generation?version=2023-05-29"
|
| 45 |
granite_headers = {
|
| 46 |
"Accept": "application/json",
|
|
@@ -128,60 +117,36 @@ def save_responses_to_pdf(responses, output_pdf_path):
|
|
| 128 |
|
| 129 |
document.build(content)
|
| 130 |
|
| 131 |
-
#
|
| 132 |
-
st.title("
|
| 133 |
|
| 134 |
-
#
|
| 135 |
-
|
|
|
|
| 136 |
|
| 137 |
-
|
| 138 |
-
|
|
|
|
|
|
|
| 139 |
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
transcription = transcribe_audio(audio_path)
|
| 162 |
-
|
| 163 |
-
# Extract text and questions from PDF
|
| 164 |
-
pdf_text, questions = extract_text_from_pdf(temp_pdf_path)
|
| 165 |
-
|
| 166 |
-
# Generate form data with Granite
|
| 167 |
-
form_data = generate_form_data(transcription, questions)
|
| 168 |
-
responses.append(form_data)
|
| 169 |
-
|
| 170 |
-
# Display responses in output box
|
| 171 |
-
output_box.write("Processing completed. Here are the results:")
|
| 172 |
-
for index, response in enumerate(responses, start=1):
|
| 173 |
-
output_box.write(f"File {index}:\n{response}\n")
|
| 174 |
-
|
| 175 |
-
# Save responses to PDF
|
| 176 |
-
save_responses_to_pdf(responses, temp_output_pdf_path)
|
| 177 |
-
|
| 178 |
-
# Button to download the PDF with responses
|
| 179 |
-
with open(temp_output_pdf_path, "rb") as f:
|
| 180 |
-
st.download_button(
|
| 181 |
-
label="Download Responses as PDF",
|
| 182 |
-
data=f,
|
| 183 |
-
file_name="response_output.pdf",
|
| 184 |
-
mime="application/pdf"
|
| 185 |
-
)
|
| 186 |
-
else:
|
| 187 |
-
st.warning("Please upload both audio files and a PDF file.")
|
|
|
|
| 1 |
+
import streamlit as st
|
|
|
|
| 2 |
import requests
|
| 3 |
import pdfplumber
|
| 4 |
import torch
|
|
|
|
| 5 |
from transformers import AutoModelForSpeechSeq2Seq, AutoProcessor, pipeline
|
|
|
|
| 6 |
from reportlab.lib.pagesizes import letter
|
|
|
|
|
|
|
| 7 |
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer
|
| 8 |
+
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
| 9 |
+
import os
|
| 10 |
|
| 11 |
+
# Define paths (for temporary storage)
|
| 12 |
+
audio_folder_path = "./audio" # Temporary path for uploaded files
|
| 13 |
+
pdf_path = "./form.pdf" # Temporary path for uploaded files
|
| 14 |
+
output_pdf_path = "./response_output.pdf" # Path to save the PDF
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
# Setup models
|
| 17 |
device = "cuda:0" if torch.cuda.is_available() else "cpu"
|
|
|
|
| 30 |
device=device
|
| 31 |
)
|
| 32 |
|
|
|
|
| 33 |
granite_url = "https://us-south.ml.cloud.ibm.com/ml/v1/text/generation?version=2023-05-29"
|
| 34 |
granite_headers = {
|
| 35 |
"Accept": "application/json",
|
|
|
|
| 117 |
|
| 118 |
document.build(content)
|
| 119 |
|
| 120 |
+
# Streamlit UI
|
| 121 |
+
st.title("Audio to Form Data Processing")
|
| 122 |
|
| 123 |
+
# File upload
|
| 124 |
+
uploaded_audio = st.file_uploader("Upload Audio File", type=["wav", "mp3"])
|
| 125 |
+
uploaded_pdf = st.file_uploader("Upload PDF File", type=["pdf"])
|
| 126 |
|
| 127 |
+
if uploaded_audio and uploaded_pdf:
|
| 128 |
+
# Save uploaded files temporarily
|
| 129 |
+
audio_path = os.path.join(audio_folder_path, uploaded_audio.name)
|
| 130 |
+
pdf_path = os.path.join(pdf_path, uploaded_pdf.name)
|
| 131 |
|
| 132 |
+
with open(audio_path, "wb") as f:
|
| 133 |
+
f.write(uploaded_audio.read())
|
| 134 |
+
|
| 135 |
+
with open(pdf_path, "wb") as f:
|
| 136 |
+
f.write(uploaded_pdf.read())
|
| 137 |
+
|
| 138 |
+
# Process files
|
| 139 |
+
transcribed_text = transcribe_audio(audio_path)
|
| 140 |
+
pdf_text, pdf_questions = extract_text_from_pdf(pdf_path)
|
| 141 |
+
form_data = generate_form_data(transcribed_text, pdf_questions)
|
| 142 |
+
|
| 143 |
+
# Display results
|
| 144 |
+
st.write("### Extracted Form Data")
|
| 145 |
+
st.text_area("Form Data", form_data, height=300)
|
| 146 |
+
|
| 147 |
+
# Save results to PDF
|
| 148 |
+
save_responses_to_pdf([form_data], output_pdf_path)
|
| 149 |
+
|
| 150 |
+
# Download link for PDF
|
| 151 |
+
with open(output_pdf_path, "rb") as f:
|
| 152 |
+
st.download_button("Download Response PDF", f, file_name="response_output.pdf")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|