Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,14 +14,13 @@ HUGGINGFACE_API_TOKEN = os.getenv("HUGGINGFACE_API_TOKEN")
|
|
| 14 |
if not GROQ_API_KEY or not HUGGINGFACE_API_TOKEN:
|
| 15 |
raise EnvironmentError("Please set GROQ_API_KEY and HUGGINGFACE_API_TOKEN.")
|
| 16 |
|
| 17 |
-
#
|
| 18 |
WHISPER_MODEL = "openai/whisper-large-v3-turbo"
|
| 19 |
-
SUMMARIZER_MODEL = "facebook/bart-large-cnn"
|
| 20 |
TUTORIAL_MODEL = "openai/gpt-oss-120b"
|
| 21 |
|
| 22 |
# API Clients
|
| 23 |
groq_client = Groq(api_key=GROQ_API_KEY)
|
| 24 |
-
hf_client = InferenceClient(token=HUGGINGFACE_API_TOKEN)
|
| 25 |
|
| 26 |
# ========= AUDIO HELPERS ==========
|
| 27 |
|
|
@@ -72,21 +71,13 @@ def transcribe_audio(audio_path=None, youtube_url=None):
|
|
| 72 |
elif audio_path:
|
| 73 |
wav_file = convert_to_wav(audio_path)
|
| 74 |
else:
|
| 75 |
-
return "⚠️ Please upload or record an audio or provide YouTube link."
|
| 76 |
-
|
| 77 |
-
with open(wav_file, "rb") as
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
headers={"Authorization": f"Bearer {HUGGINGFACE_API_TOKEN}"},
|
| 81 |
-
data=audio_file.read(),
|
| 82 |
-
)
|
| 83 |
-
if response.status_code != 200:
|
| 84 |
-
return f"❌ Transcription failed: {response.text}"
|
| 85 |
-
result = response.json()
|
| 86 |
-
text = result.get("text", "").strip()
|
| 87 |
-
if not text:
|
| 88 |
return "❌ No text generated from transcription."
|
| 89 |
-
return
|
| 90 |
except Exception as e:
|
| 91 |
return f"❌ Error during transcription: {e}"
|
| 92 |
|
|
@@ -98,30 +89,30 @@ def summarize_text(text, language):
|
|
| 98 |
|
| 99 |
prompt = (
|
| 100 |
f"Summarize the following text in detail in {language}. "
|
| 101 |
-
f"Ensure the summary is coherent and
|
| 102 |
)
|
| 103 |
summary = groq_client.chat.completions.create(
|
| 104 |
model="llama-3.1-8b-instant",
|
| 105 |
messages=[{"role": "user", "content": prompt}],
|
| 106 |
temperature=0.6,
|
| 107 |
-
max_tokens=
|
| 108 |
)
|
| 109 |
return summary.choices[0].message.content.strip()
|
| 110 |
except Exception as e:
|
| 111 |
return f"❌ Summarization failed: {e}"
|
| 112 |
|
| 113 |
def generate_tutorial(summary_text, language):
|
| 114 |
-
"""Generate
|
| 115 |
try:
|
| 116 |
prompt = (
|
| 117 |
f"Create a simple tutorial for absolute beginners based on this summary. "
|
| 118 |
-
f"Write
|
| 119 |
)
|
| 120 |
tutorial = groq_client.chat.completions.create(
|
| 121 |
model="llama-3.1-8b-instant",
|
| 122 |
messages=[{"role": "user", "content": prompt}],
|
| 123 |
temperature=0.7,
|
| 124 |
-
max_tokens=
|
| 125 |
)
|
| 126 |
return tutorial.choices[0].message.content.strip()
|
| 127 |
except Exception as e:
|
|
@@ -129,14 +120,14 @@ def generate_tutorial(summary_text, language):
|
|
| 129 |
|
| 130 |
# ========= GRADIO INTERFACE ==========
|
| 131 |
|
| 132 |
-
with gr.Blocks(theme=gr.themes.Soft(primary_hue="
|
| 133 |
gr.Markdown(
|
| 134 |
"""
|
| 135 |
# 🎙️ Smart Transcriber & Tutor
|
| 136 |
-
**
|
| 137 |
- Upload / Record / YouTube Transcription
|
| 138 |
-
-
|
| 139 |
-
-
|
| 140 |
"""
|
| 141 |
)
|
| 142 |
|
|
|
|
| 14 |
if not GROQ_API_KEY or not HUGGINGFACE_API_TOKEN:
|
| 15 |
raise EnvironmentError("Please set GROQ_API_KEY and HUGGINGFACE_API_TOKEN.")
|
| 16 |
|
| 17 |
+
# Models
|
| 18 |
WHISPER_MODEL = "openai/whisper-large-v3-turbo"
|
|
|
|
| 19 |
TUTORIAL_MODEL = "openai/gpt-oss-120b"
|
| 20 |
|
| 21 |
# API Clients
|
| 22 |
groq_client = Groq(api_key=GROQ_API_KEY)
|
| 23 |
+
hf_client = InferenceClient(model=WHISPER_MODEL, token=HUGGINGFACE_API_TOKEN)
|
| 24 |
|
| 25 |
# ========= AUDIO HELPERS ==========
|
| 26 |
|
|
|
|
| 71 |
elif audio_path:
|
| 72 |
wav_file = convert_to_wav(audio_path)
|
| 73 |
else:
|
| 74 |
+
return "⚠️ Please upload or record an audio or provide a YouTube link."
|
| 75 |
+
|
| 76 |
+
with open(wav_file, "rb") as f:
|
| 77 |
+
transcription = hf_client.audio_to_text(f)
|
| 78 |
+
if not transcription:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
return "❌ No text generated from transcription."
|
| 80 |
+
return transcription.strip()
|
| 81 |
except Exception as e:
|
| 82 |
return f"❌ Error during transcription: {e}"
|
| 83 |
|
|
|
|
| 89 |
|
| 90 |
prompt = (
|
| 91 |
f"Summarize the following text in detail in {language}. "
|
| 92 |
+
f"Ensure the summary is coherent, complete, and descriptive:\n\n{text}"
|
| 93 |
)
|
| 94 |
summary = groq_client.chat.completions.create(
|
| 95 |
model="llama-3.1-8b-instant",
|
| 96 |
messages=[{"role": "user", "content": prompt}],
|
| 97 |
temperature=0.6,
|
| 98 |
+
max_tokens=1800,
|
| 99 |
)
|
| 100 |
return summary.choices[0].message.content.strip()
|
| 101 |
except Exception as e:
|
| 102 |
return f"❌ Summarization failed: {e}"
|
| 103 |
|
| 104 |
def generate_tutorial(summary_text, language):
|
| 105 |
+
"""Generate a clear tutorial based on summary."""
|
| 106 |
try:
|
| 107 |
prompt = (
|
| 108 |
f"Create a simple tutorial for absolute beginners based on this summary. "
|
| 109 |
+
f"Write in {language} language, use step-by-step explanations, examples, and clear structure:\n\n{summary_text}"
|
| 110 |
)
|
| 111 |
tutorial = groq_client.chat.completions.create(
|
| 112 |
model="llama-3.1-8b-instant",
|
| 113 |
messages=[{"role": "user", "content": prompt}],
|
| 114 |
temperature=0.7,
|
| 115 |
+
max_tokens=2000,
|
| 116 |
)
|
| 117 |
return tutorial.choices[0].message.content.strip()
|
| 118 |
except Exception as e:
|
|
|
|
| 120 |
|
| 121 |
# ========= GRADIO INTERFACE ==========
|
| 122 |
|
| 123 |
+
with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as app:
|
| 124 |
gr.Markdown(
|
| 125 |
"""
|
| 126 |
# 🎙️ Smart Transcriber & Tutor
|
| 127 |
+
**Capabilities:**
|
| 128 |
- Upload / Record / YouTube Transcription
|
| 129 |
+
- Generate Detailed Summaries (Urdu / English)
|
| 130 |
+
- Create Step-by-Step Tutorials for Beginners
|
| 131 |
"""
|
| 132 |
)
|
| 133 |
|