Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,16 +2,22 @@ import gradio as gr
|
|
2 |
import requests
|
3 |
from fpdf import FPDF
|
4 |
import nltk
|
|
|
5 |
from nltk.tokenize import sent_tokenize
|
6 |
import random
|
7 |
-
import os
|
8 |
|
9 |
-
#
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
# Function to send audio to Groq API and get transcription
|
13 |
def transcribe(audio_path):
|
14 |
-
# Read audio file in binary mode
|
15 |
with open(audio_path, "rb") as audio_file:
|
16 |
audio_data = audio_file.read()
|
17 |
|
@@ -19,7 +25,6 @@ def transcribe(audio_path):
|
|
19 |
headers = {
|
20 |
"Authorization": "Bearer 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", # Replace with your actual API key
|
21 |
}
|
22 |
-
|
23 |
files = {
|
24 |
'file': ('audio.wav', audio_data, 'audio/wav'),
|
25 |
}
|
@@ -29,7 +34,6 @@ def transcribe(audio_path):
|
|
29 |
'language': 'en',
|
30 |
}
|
31 |
|
32 |
-
# Send audio to Groq API
|
33 |
response = requests.post(groq_api_endpoint, headers=headers, files=files, data=data)
|
34 |
|
35 |
if response.status_code == 200:
|
@@ -41,16 +45,15 @@ def transcribe(audio_path):
|
|
41 |
print(f"API Error: {error_msg}")
|
42 |
return create_error_pdf(f"API Error: {error_msg}")
|
43 |
|
44 |
-
# Function to generate notes and questions
|
45 |
def generate_notes(transcript):
|
46 |
-
|
47 |
-
|
|
|
|
|
48 |
|
49 |
-
# Generate long and short questions
|
50 |
long_questions = [f"What is meant by '{sentence}'?" for sentence in sentences[:5]]
|
51 |
short_questions = [f"Define '{sentence.split()[0]}'." for sentence in sentences[:5]]
|
52 |
|
53 |
-
# Generate MCQs
|
54 |
mcqs = []
|
55 |
for sentence in sentences[:5]:
|
56 |
mcq = {
|
@@ -60,38 +63,31 @@ def generate_notes(transcript):
|
|
60 |
}
|
61 |
mcqs.append(mcq)
|
62 |
|
63 |
-
# Create PDF
|
64 |
pdf_path = create_pdf(transcript, long_questions, short_questions, mcqs)
|
65 |
return pdf_path
|
66 |
|
67 |
-
# Function to create a PDF for transcription and questions
|
68 |
def create_pdf(transcript, long_questions, short_questions, mcqs):
|
69 |
pdf = FPDF()
|
70 |
pdf.add_page()
|
71 |
|
72 |
-
# Title
|
73 |
pdf.set_font("Arial", "B", 16)
|
74 |
pdf.cell(200, 10, "Transcription Notes", ln=True, align="C")
|
75 |
|
76 |
-
# Transcription
|
77 |
pdf.set_font("Arial", "", 12)
|
78 |
pdf.multi_cell(0, 10, f"Transcription:\n{transcript}\n\n")
|
79 |
|
80 |
-
# Long Questions
|
81 |
pdf.set_font("Arial", "B", 14)
|
82 |
pdf.cell(200, 10, "Long Questions", ln=True)
|
83 |
pdf.set_font("Arial", "", 12)
|
84 |
for question in long_questions:
|
85 |
pdf.multi_cell(0, 10, f"- {question}\n")
|
86 |
|
87 |
-
# Short Questions
|
88 |
pdf.set_font("Arial", "B", 14)
|
89 |
pdf.cell(200, 10, "Short Questions", ln=True)
|
90 |
pdf.set_font("Arial", "", 12)
|
91 |
for question in short_questions:
|
92 |
pdf.multi_cell(0, 10, f"- {question}\n")
|
93 |
|
94 |
-
# MCQs
|
95 |
pdf.set_font("Arial", "B", 14)
|
96 |
pdf.cell(200, 10, "Multiple Choice Questions (MCQs)", ln=True)
|
97 |
pdf.set_font("Arial", "", 12)
|
@@ -101,13 +97,11 @@ def create_pdf(transcript, long_questions, short_questions, mcqs):
|
|
101 |
pdf.multi_cell(0, 10, f" - {option}")
|
102 |
pdf.multi_cell(0, 10, f"Answer: {mcq['answer']}\n")
|
103 |
|
104 |
-
# Save PDF
|
105 |
pdf_path = "/mnt/data/transcription_notes.pdf"
|
106 |
pdf.output(pdf_path)
|
107 |
|
108 |
return pdf_path
|
109 |
|
110 |
-
# Function to create an error PDF
|
111 |
def create_error_pdf(message):
|
112 |
pdf = FPDF()
|
113 |
pdf.add_page()
|
@@ -120,7 +114,6 @@ def create_error_pdf(message):
|
|
120 |
pdf.output(error_pdf_path)
|
121 |
return error_pdf_path
|
122 |
|
123 |
-
# Gradio interface
|
124 |
iface = gr.Interface(
|
125 |
fn=transcribe,
|
126 |
inputs=gr.Audio(type="filepath"),
|
|
|
2 |
import requests
|
3 |
from fpdf import FPDF
|
4 |
import nltk
|
5 |
+
import os
|
6 |
from nltk.tokenize import sent_tokenize
|
7 |
import random
|
|
|
8 |
|
9 |
+
# Attempt to download punkt tokenizer
|
10 |
+
try:
|
11 |
+
nltk.download("punkt")
|
12 |
+
except:
|
13 |
+
print("NLTK punkt tokenizer download failed. Using custom tokenizer.")
|
14 |
+
|
15 |
+
# Custom fallback for sentence tokenization
|
16 |
+
def custom_sent_tokenize(text):
|
17 |
+
return text.split(". ")
|
18 |
|
19 |
# Function to send audio to Groq API and get transcription
|
20 |
def transcribe(audio_path):
|
|
|
21 |
with open(audio_path, "rb") as audio_file:
|
22 |
audio_data = audio_file.read()
|
23 |
|
|
|
25 |
headers = {
|
26 |
"Authorization": "Bearer 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", # Replace with your actual API key
|
27 |
}
|
|
|
28 |
files = {
|
29 |
'file': ('audio.wav', audio_data, 'audio/wav'),
|
30 |
}
|
|
|
34 |
'language': 'en',
|
35 |
}
|
36 |
|
|
|
37 |
response = requests.post(groq_api_endpoint, headers=headers, files=files, data=data)
|
38 |
|
39 |
if response.status_code == 200:
|
|
|
45 |
print(f"API Error: {error_msg}")
|
46 |
return create_error_pdf(f"API Error: {error_msg}")
|
47 |
|
|
|
48 |
def generate_notes(transcript):
|
49 |
+
try:
|
50 |
+
sentences = sent_tokenize(transcript)
|
51 |
+
except LookupError:
|
52 |
+
sentences = custom_sent_tokenize(transcript)
|
53 |
|
|
|
54 |
long_questions = [f"What is meant by '{sentence}'?" for sentence in sentences[:5]]
|
55 |
short_questions = [f"Define '{sentence.split()[0]}'." for sentence in sentences[:5]]
|
56 |
|
|
|
57 |
mcqs = []
|
58 |
for sentence in sentences[:5]:
|
59 |
mcq = {
|
|
|
63 |
}
|
64 |
mcqs.append(mcq)
|
65 |
|
|
|
66 |
pdf_path = create_pdf(transcript, long_questions, short_questions, mcqs)
|
67 |
return pdf_path
|
68 |
|
|
|
69 |
def create_pdf(transcript, long_questions, short_questions, mcqs):
|
70 |
pdf = FPDF()
|
71 |
pdf.add_page()
|
72 |
|
|
|
73 |
pdf.set_font("Arial", "B", 16)
|
74 |
pdf.cell(200, 10, "Transcription Notes", ln=True, align="C")
|
75 |
|
|
|
76 |
pdf.set_font("Arial", "", 12)
|
77 |
pdf.multi_cell(0, 10, f"Transcription:\n{transcript}\n\n")
|
78 |
|
|
|
79 |
pdf.set_font("Arial", "B", 14)
|
80 |
pdf.cell(200, 10, "Long Questions", ln=True)
|
81 |
pdf.set_font("Arial", "", 12)
|
82 |
for question in long_questions:
|
83 |
pdf.multi_cell(0, 10, f"- {question}\n")
|
84 |
|
|
|
85 |
pdf.set_font("Arial", "B", 14)
|
86 |
pdf.cell(200, 10, "Short Questions", ln=True)
|
87 |
pdf.set_font("Arial", "", 12)
|
88 |
for question in short_questions:
|
89 |
pdf.multi_cell(0, 10, f"- {question}\n")
|
90 |
|
|
|
91 |
pdf.set_font("Arial", "B", 14)
|
92 |
pdf.cell(200, 10, "Multiple Choice Questions (MCQs)", ln=True)
|
93 |
pdf.set_font("Arial", "", 12)
|
|
|
97 |
pdf.multi_cell(0, 10, f" - {option}")
|
98 |
pdf.multi_cell(0, 10, f"Answer: {mcq['answer']}\n")
|
99 |
|
|
|
100 |
pdf_path = "/mnt/data/transcription_notes.pdf"
|
101 |
pdf.output(pdf_path)
|
102 |
|
103 |
return pdf_path
|
104 |
|
|
|
105 |
def create_error_pdf(message):
|
106 |
pdf = FPDF()
|
107 |
pdf.add_page()
|
|
|
114 |
pdf.output(error_pdf_path)
|
115 |
return error_pdf_path
|
116 |
|
|
|
117 |
iface = gr.Interface(
|
118 |
fn=transcribe,
|
119 |
inputs=gr.Audio(type="filepath"),
|