Turbiling commited on
Commit
504a5c5
·
verified ·
1 Parent(s): 7dc9781

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -10
app.py CHANGED
@@ -33,7 +33,7 @@ def download_youtube_audio(youtube_url):
33
  ydl.download([youtube_url])
34
  return tmp_file.name
35
 
36
- # ✅ Split long audio into chunks (max 5 mins)
37
  def split_audio(file_path, max_duration_ms=5*60*1000):
38
  audio = AudioSegment.from_file(file_path)
39
  chunks = []
@@ -44,7 +44,7 @@ def split_audio(file_path, max_duration_ms=5*60*1000):
44
  chunks.append(temp_chunk.name)
45
  return chunks
46
 
47
- # ✅ Transcribe with Groq (chunk-wise)
48
  def transcribe_audio(audio_path):
49
  try:
50
  chunks = split_audio(audio_path)
@@ -60,23 +60,29 @@ def transcribe_audio(audio_path):
60
  except Exception as e:
61
  return f"❌ Error during transcription: {e}"
62
 
63
- # ✅ Summarize
64
  def summarize_text(text, lang):
65
  try:
66
  if lang == "English":
67
  model = "facebook/bart-large-cnn"
68
- prompt = f"Summarize the following text in English:\n\n{text}"
69
  else:
70
  model = "facebook/mbart-large-50-many-to-many-mmt"
71
- prompt = f"مندرجہ ذیل عبارت کا جامع اردو خلاصہ تحریر کریں:\n\n{text}"
72
 
73
- output = hf_client.text_generation(
74
  model=model,
75
- prompt=prompt,
76
- max_new_tokens=250,
 
77
  temperature=0.7,
78
  )
79
- return output
 
 
 
 
 
80
  except Exception as e:
81
  return f"❌ Error during summarization: {e}"
82
 
@@ -98,7 +104,7 @@ def process_input(youtube_url, audio_file, lang):
98
 
99
  # ✅ Gradio Interface
100
  with gr.Blocks(title="🎧 Urdu/English Audio Summarizer") as app:
101
- gr.Markdown("## 🎧 Urdu & English Audio Summarizer\nUpload audio or paste YouTube link below:")
102
 
103
  with gr.Row():
104
  youtube_link = gr.Textbox(label="📺 YouTube Link (optional)")
 
33
  ydl.download([youtube_url])
34
  return tmp_file.name
35
 
36
+ # ✅ Split long audio into 5-minute chunks
37
  def split_audio(file_path, max_duration_ms=5*60*1000):
38
  audio = AudioSegment.from_file(file_path)
39
  chunks = []
 
44
  chunks.append(temp_chunk.name)
45
  return chunks
46
 
47
+ # ✅ Transcribe with Groq Whisper
48
  def transcribe_audio(audio_path):
49
  try:
50
  chunks = split_audio(audio_path)
 
60
  except Exception as e:
61
  return f"❌ Error during transcription: {e}"
62
 
63
+ # ✅ Summarize with correct API
64
  def summarize_text(text, lang):
65
  try:
66
  if lang == "English":
67
  model = "facebook/bart-large-cnn"
68
+ inputs = text
69
  else:
70
  model = "facebook/mbart-large-50-many-to-many-mmt"
71
+ inputs = f"مندرجہ ذیل عبارت کا جامع اردو خلاصہ لکھیں:\n\n{text}"
72
 
73
+ result = hf_client.summarization(
74
  model=model,
75
+ inputs=inputs,
76
+ max_length=250,
77
+ min_length=80,
78
  temperature=0.7,
79
  )
80
+ if isinstance(result, list):
81
+ return result[0]["summary_text"]
82
+ elif isinstance(result, dict) and "summary_text" in result:
83
+ return result["summary_text"]
84
+ else:
85
+ return str(result)
86
  except Exception as e:
87
  return f"❌ Error during summarization: {e}"
88
 
 
104
 
105
  # ✅ Gradio Interface
106
  with gr.Blocks(title="🎧 Urdu/English Audio Summarizer") as app:
107
+ gr.Markdown("## 🎧 Urdu & English Audio Summarizer\nUpload an audio file or paste a YouTube link below:")
108
 
109
  with gr.Row():
110
  youtube_link = gr.Textbox(label="📺 YouTube Link (optional)")