ShahadFawaz99
commited on
Commit
•
68f92dc
1
Parent(s):
68c2246
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import whisper # Library for speech recognition
|
3 |
+
from transformers import pipeline
|
4 |
+
import pandas as pd
|
5 |
+
|
6 |
+
|
7 |
+
# Load the Whisper model for speech recognition
|
8 |
+
whisper_model = whisper.load_model("base")
|
9 |
+
|
10 |
+
# Load the summarization model from Hugging Face
|
11 |
+
summarization = pipeline("summarization", model="google/pegasus-large")
|
12 |
+
|
13 |
+
def process_audio(audio_file, min_length, max_length):
|
14 |
+
try:
|
15 |
+
# Ensure audio_file is not None and has valid content
|
16 |
+
if audio_file is None:
|
17 |
+
raise ValueError("No audio file provided.")
|
18 |
+
|
19 |
+
# Use the Whisper model to transcribe the audio file into text
|
20 |
+
result = whisper_model.transcribe(audio_file)
|
21 |
+
text = result['text']
|
22 |
+
|
23 |
+
# Check if transcription was successful
|
24 |
+
if not text:
|
25 |
+
raise ValueError("Failed to transcribe the audio. The transcription result is empty.")
|
26 |
+
|
27 |
+
# Use the summarization pipeline to summarize the transcribed text
|
28 |
+
summary_result = summarization(text, min_length=min_length, max_length=max_length)
|
29 |
+
summary = summary_result[0]['summary_text']
|
30 |
+
|
31 |
+
# Check if summarization was successful
|
32 |
+
if not summary:
|
33 |
+
raise ValueError("Failed to summarize the transcript. The summary result is empty.")
|
34 |
+
|
35 |
+
# Create a DataFrame to store the audio file, transcript, and summary
|
36 |
+
df_results = pd.DataFrame({
|
37 |
+
"Audio File": [audio_file], # Store the path to the audio file
|
38 |
+
"Transcript": [text], # Store the transcribed text
|
39 |
+
"Summary": [summary] # Store the generated summary
|
40 |
+
})
|
41 |
+
|
42 |
+
# Save the results to a CSV file named "results.csv"
|
43 |
+
df_results.to_csv("results.csv", index=False)
|
44 |
+
|
45 |
+
# Return the transcript and summary to be displayed in the Gradio interface
|
46 |
+
return text, summary
|
47 |
+
|
48 |
+
except Exception as e:
|
49 |
+
# General error handling
|
50 |
+
error_message = f"An error occurred: {str(e)}"
|
51 |
+
return error_message, error_message
|
52 |
+
|
53 |
+
# Create a Gradio interface
|
54 |
+
iface = gr.Interface(
|
55 |
+
fn=process_audio, # The function to be called when processing the input
|
56 |
+
inputs=[
|
57 |
+
gr.Audio(sources="upload", type="filepath", label="Upload your audio file"), # Audio input field for file upload
|
58 |
+
gr.Slider(minimum=10, maximum=50, value=30, label="Minimum Summary Length"), # Slider for setting minimum summary length
|
59 |
+
gr.Slider(minimum=50, maximum=600, value=100, label="Maximum Summary Length") # Slider for setting maximum summary length
|
60 |
+
],
|
61 |
+
outputs=[
|
62 |
+
gr.Textbox(label="Transcript"), # Textbox for displaying the transcript
|
63 |
+
gr.Textbox(label="Summary") # Textbox for displaying the summary
|
64 |
+
],
|
65 |
+
title="Audio to Summarized Transcript", # Title of the app
|
66 |
+
description="Upload an audio file and adjust summary length to get both the transcript and summary." # Description of the app
|
67 |
+
)
|
68 |
+
|
69 |
+
# Launch the app
|
70 |
+
iface.launch()
|