umarmajeedofficial commited on
Commit
d3242ce
·
verified ·
1 Parent(s): 2438562

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -72
app.py CHANGED
@@ -1,27 +1,17 @@
1
- import io
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
- # Suppress warnings
15
- import warnings
16
- warnings.filterwarnings("ignore")
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
- # Set up the Streamlit app
132
- st.title("FILL IT")
133
 
134
- # Upload multiple audio files
135
- uploaded_audios = st.file_uploader("Upload audio files", type=["wav", "mp3"], accept_multiple_files=True)
 
136
 
137
- # Upload PDF file
138
- uploaded_pdf = st.file_uploader("Upload a PDF file with questions", type=["pdf"])
 
 
139
 
140
- # Output box to display responses
141
- output_box = st.empty()
142
-
143
- # Button to start processing
144
- if st.button("Start Processing"):
145
- if uploaded_audios and uploaded_pdf:
146
- responses = []
147
-
148
- # Read uploaded PDF file
149
- pdf_bytes = uploaded_pdf.read()
150
- with open(temp_pdf_path, "wb") as f:
151
- f.write(pdf_bytes)
152
-
153
- # Process each uploaded audio file
154
- for audio_file in uploaded_audios:
155
- audio_bytes = audio_file.read()
156
- audio_path = os.path.join(temp_audio_folder, audio_file.name)
157
- with open(audio_path, "wb") as f:
158
- f.write(audio_bytes)
159
-
160
- # Transcribe audio
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")