Update app.py
Browse files
app.py
CHANGED
|
@@ -1,116 +1,46 @@
|
|
| 1 |
-
# import gradio as gr
|
| 2 |
-
# import whisper
|
| 3 |
-
# import os
|
| 4 |
-
# from groq import Groq
|
| 5 |
-
|
| 6 |
-
# # 🔐 Get Groq API key securely from Hugging Face Secrets
|
| 7 |
-
# GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 8 |
-
# groq_client = Groq(api_key=GROQ_API_KEY)
|
| 9 |
-
# MODEL_NAME = "llama3-8b-8192"
|
| 10 |
-
|
| 11 |
-
# # 🎙 Load Whisper
|
| 12 |
-
# transcriber = whisper.load_model("base")
|
| 13 |
-
|
| 14 |
-
# def transcribe_and_summarize(audio):
|
| 15 |
-
# # Step 1: Transcribe + Detect Language
|
| 16 |
-
# result = transcriber.transcribe(audio)
|
| 17 |
-
# transcript = result["text"]
|
| 18 |
-
# detected_lang = result["language"]
|
| 19 |
-
|
| 20 |
-
# # Step 2: Summarize in the same language
|
| 21 |
-
# if detected_lang == "en":
|
| 22 |
-
# system_prompt = "You are an expert English summarizer."
|
| 23 |
-
# user_prompt = f"Please summarize the following English text:\n\n{transcript}"
|
| 24 |
-
# elif detected_lang == "ur":
|
| 25 |
-
# system_prompt = "آپ ایک ماہر خلاصہ نگار ہیں جو اردو میں خلاصہ فراہم کرتے ہیں۔"
|
| 26 |
-
# user_prompt = f"براہ کرم مندرجہ ذیل اردو متن کا خلاصہ فراہم کریں:\n\n{transcript}"
|
| 27 |
-
# else:
|
| 28 |
-
# system_prompt = "You are a helpful summarizer."
|
| 29 |
-
# user_prompt = f"Summarize this text:\n\n{transcript}"
|
| 30 |
-
|
| 31 |
-
# response = groq_client.chat.completions.create(
|
| 32 |
-
# model=MODEL_NAME,
|
| 33 |
-
# messages=[
|
| 34 |
-
# {"role": "system", "content": system_prompt},
|
| 35 |
-
# {"role": "user", "content": user_prompt}
|
| 36 |
-
# ]
|
| 37 |
-
# )
|
| 38 |
-
# summary = response.choices[0].message.content.strip()
|
| 39 |
-
|
| 40 |
-
# lang_label = "English" if detected_lang == "en" else "Urdu" if detected_lang == "ur" else detected_lang.upper()
|
| 41 |
-
|
| 42 |
-
# return f"[{lang_label}] {transcript}", f"[{lang_label}] {summary}"
|
| 43 |
-
|
| 44 |
-
# demo = gr.Interface(
|
| 45 |
-
# fn=transcribe_and_summarize,
|
| 46 |
-
# inputs=gr.Audio(type="filepath", label="🎧 Upload Audio (English or Urdu)"),
|
| 47 |
-
# outputs=[
|
| 48 |
-
# gr.Textbox(label="📝 Transcript"),
|
| 49 |
-
# gr.Textbox(label="🧠 Summary")
|
| 50 |
-
# ],
|
| 51 |
-
# title="🗣️ Multilingual Audio Summarizer",
|
| 52 |
-
# description="Upload English or Urdu audio. The app transcribes and summarizes in the same language using Whisper + Groq."
|
| 53 |
-
# )
|
| 54 |
-
|
| 55 |
-
# demo.launch()
|
| 56 |
-
|
| 57 |
-
|
| 58 |
import gradio as gr
|
|
|
|
| 59 |
import os
|
| 60 |
from groq import Groq
|
| 61 |
-
from transformers import pipeline
|
| 62 |
-
import torchaudio
|
| 63 |
|
| 64 |
-
# 🔐 Groq API key from
|
| 65 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 66 |
groq_client = Groq(api_key=GROQ_API_KEY)
|
| 67 |
MODEL_NAME = "llama3-8b-8192"
|
| 68 |
|
| 69 |
-
#
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
-
response = groq_client.chat.completions.create(
|
| 100 |
-
model=MODEL_NAME,
|
| 101 |
-
messages=[
|
| 102 |
-
{"role": "system", "content": system_prompt},
|
| 103 |
-
{"role": "user", "content": user_prompt}
|
| 104 |
-
]
|
| 105 |
-
)
|
| 106 |
-
summary = response.choices[0].message.content.strip()
|
| 107 |
-
lang_label = "English" if detected_lang == "en" else "Urdu" if detected_lang == "ur" else detected_lang.upper()
|
| 108 |
-
return f"[{lang_label}] {transcript}", f"[{lang_label}] {summary}"
|
| 109 |
-
|
| 110 |
-
except Exception as e:
|
| 111 |
-
return "❌ Error processing audio. Please try again.", str(e)
|
| 112 |
-
|
| 113 |
-
# Gradio UI
|
| 114 |
demo = gr.Interface(
|
| 115 |
fn=transcribe_and_summarize,
|
| 116 |
inputs=gr.Audio(type="filepath", label="🎧 Upload Audio (English or Urdu)"),
|
|
@@ -119,7 +49,9 @@ demo = gr.Interface(
|
|
| 119 |
gr.Textbox(label="🧠 Summary")
|
| 120 |
],
|
| 121 |
title="🗣️ Multilingual Audio Summarizer",
|
| 122 |
-
description="Upload English or Urdu audio
|
| 123 |
)
|
| 124 |
|
| 125 |
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import whisper
|
| 3 |
import os
|
| 4 |
from groq import Groq
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
# 🔐 Get Groq API key securely from Hugging Face Secrets
|
| 7 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 8 |
groq_client = Groq(api_key=GROQ_API_KEY)
|
| 9 |
MODEL_NAME = "llama3-8b-8192"
|
| 10 |
|
| 11 |
+
# 🎙 Load Whisper
|
| 12 |
+
transcriber = whisper.load_model("base")
|
| 13 |
+
|
| 14 |
+
def transcribe_and_summarize(audio):
|
| 15 |
+
# Step 1: Transcribe + Detect Language
|
| 16 |
+
result = transcriber.transcribe(audio)
|
| 17 |
+
transcript = result["text"]
|
| 18 |
+
detected_lang = result["language"]
|
| 19 |
+
|
| 20 |
+
# Step 2: Summarize in the same language
|
| 21 |
+
if detected_lang == "en":
|
| 22 |
+
system_prompt = "You are an expert English summarizer."
|
| 23 |
+
user_prompt = f"Please summarize the following English text:\n\n{transcript}"
|
| 24 |
+
elif detected_lang == "ur":
|
| 25 |
+
system_prompt = "آپ ایک ماہر خلاصہ نگار ہیں جو اردو میں خلاصہ فراہم کرتے ہیں۔"
|
| 26 |
+
user_prompt = f"براہ کرم مندرجہ ذیل اردو متن کا خلاصہ فراہم کریں:\n\n{transcript}"
|
| 27 |
+
else:
|
| 28 |
+
system_prompt = "You are a helpful summarizer."
|
| 29 |
+
user_prompt = f"Summarize this text:\n\n{transcript}"
|
| 30 |
+
|
| 31 |
+
response = groq_client.chat.completions.create(
|
| 32 |
+
model=MODEL_NAME,
|
| 33 |
+
messages=[
|
| 34 |
+
{"role": "system", "content": system_prompt},
|
| 35 |
+
{"role": "user", "content": user_prompt}
|
| 36 |
+
]
|
| 37 |
+
)
|
| 38 |
+
summary = response.choices[0].message.content.strip()
|
| 39 |
+
|
| 40 |
+
lang_label = "English" if detected_lang == "en" else "Urdu" if detected_lang == "ur" else detected_lang.upper()
|
| 41 |
+
|
| 42 |
+
return f"[{lang_label}] {transcript}", f"[{lang_label}] {summary}"
|
| 43 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
demo = gr.Interface(
|
| 45 |
fn=transcribe_and_summarize,
|
| 46 |
inputs=gr.Audio(type="filepath", label="🎧 Upload Audio (English or Urdu)"),
|
|
|
|
| 49 |
gr.Textbox(label="🧠 Summary")
|
| 50 |
],
|
| 51 |
title="🗣️ Multilingual Audio Summarizer",
|
| 52 |
+
description="Upload English or Urdu audio. The app transcribes and summarizes in the same language using Whisper + Groq."
|
| 53 |
)
|
| 54 |
|
| 55 |
demo.launch()
|
| 56 |
+
|
| 57 |
+
|