Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,173 +1,26 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import speech_recognition as sr
|
| 3 |
-
from pydub import AudioSegment
|
| 4 |
-
import tempfile
|
| 5 |
-
from langdetect import detect
|
| 6 |
import os
|
| 7 |
import asyncio
|
| 8 |
from telegram import Update
|
| 9 |
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes
|
|
|
|
| 10 |
|
| 11 |
# Telegram bot token (to be set via Hugging Face Space secrets)
|
| 12 |
TELEGRAM_BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
|
| 13 |
|
| 14 |
-
#
|
| 15 |
-
|
| 16 |
-
recognizer = sr.Recognizer()
|
| 17 |
-
|
| 18 |
-
# Convert all audio inputs to WAV format
|
| 19 |
-
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as temp_file:
|
| 20 |
-
if isinstance(audio_input, tuple): # Recorded audio (sample_rate, numpy_array)
|
| 21 |
-
sample_rate, audio_data = audio_input
|
| 22 |
-
AudioSegment(audio_data, sample_rate=sample_rate, frame_rate=sample_rate, channels=1).export(temp_file.name, format="wav")
|
| 23 |
-
else: # Uploaded audio file (file path or Telegram file)
|
| 24 |
-
audio = AudioSegment.from_file(audio_input)
|
| 25 |
-
audio = audio.set_channels(1) # Convert to mono for consistency
|
| 26 |
-
audio.export(temp_file.name, format="wav")
|
| 27 |
-
audio_file_path = temp_file.name
|
| 28 |
-
|
| 29 |
-
# Debug: Check if the WAV file is valid
|
| 30 |
-
if os.path.getsize(audio_file_path) == 0:
|
| 31 |
-
raise ValueError("The converted WAV file is empty. The input audio may be corrupted.")
|
| 32 |
-
|
| 33 |
-
# Transcribe the WAV file using pocketsphinx (offline)
|
| 34 |
-
with sr.AudioFile(audio_file_path) as source:
|
| 35 |
-
audio = recognizer.record(source)
|
| 36 |
-
try:
|
| 37 |
-
transcription = recognizer.recognize_sphinx(audio) # Use pocketsphinx for offline transcription
|
| 38 |
-
except sr.UnknownValueError:
|
| 39 |
-
transcription = "Could not understand the audio."
|
| 40 |
-
except sr.RequestError as e:
|
| 41 |
-
transcription = f"Transcription failed: {str(e)}"
|
| 42 |
-
|
| 43 |
-
# Detect language
|
| 44 |
-
try:
|
| 45 |
-
language = detect(transcription)
|
| 46 |
-
except:
|
| 47 |
-
language = "Unknown"
|
| 48 |
-
|
| 49 |
-
# Save transcription to a text file
|
| 50 |
-
with tempfile.NamedTemporaryFile(suffix=".txt", delete=False, mode='w') as text_file:
|
| 51 |
-
text_file.write(transcription)
|
| 52 |
-
text_file_path = text_file.name
|
| 53 |
-
|
| 54 |
-
# Clean up temporary WAV file
|
| 55 |
-
if os.path.exists(audio_file_path):
|
| 56 |
-
os.remove(audio_file_path)
|
| 57 |
-
|
| 58 |
-
return language, transcription, text_file_path
|
| 59 |
-
|
| 60 |
-
# Gradio interface function
|
| 61 |
-
def audio_transcriptor(audio_file, audio_record):
|
| 62 |
-
if audio_file:
|
| 63 |
-
language, transcription, text_file = process_audio(audio_file)
|
| 64 |
-
elif audio_record:
|
| 65 |
-
language, transcription, text_file = process_audio(audio_record)
|
| 66 |
-
else:
|
| 67 |
-
return "Please upload an audio file or record audio.", "", None
|
| 68 |
-
|
| 69 |
-
return language, transcription, text_file
|
| 70 |
|
| 71 |
# Telegram bot handlers
|
| 72 |
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 73 |
-
await update.message.reply_text("Hello!
|
| 74 |
-
|
| 75 |
-
async def handle_audio(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 76 |
-
# Download the audio file from Telegram
|
| 77 |
-
audio_file = await update.message.audio.get_file()
|
| 78 |
-
audio_path = f"/tmp/{audio_file.file_id}.ogg" # Telegram audio files are typically in OGG format
|
| 79 |
-
await audio_file.download_to_drive(audio_path)
|
| 80 |
-
|
| 81 |
-
# Process the audio using the existing transcriptor function
|
| 82 |
-
language, transcription, text_file_path = process_audio(audio_path)
|
| 83 |
-
|
| 84 |
-
# Send the transcription back to the user
|
| 85 |
-
await update.message.reply_text(f"Detected Language: {language}\nTranscription: {transcription}")
|
| 86 |
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
| 90 |
|
| 91 |
-
|
| 92 |
-
if os.path.exists(audio_path):
|
| 93 |
-
os.remove(audio_path)
|
| 94 |
-
if os.path.exists(text_file_path):
|
| 95 |
-
os.remove(text_file_path)
|
| 96 |
-
|
| 97 |
-
# Custom HTML for styled transcription display (for Gradio interface)
|
| 98 |
-
transcription_html = """
|
| 99 |
-
<div class="transcription-container" id="transcriptionContainer">
|
| 100 |
-
<h2>Transcription Results</h2>
|
| 101 |
-
<div class="language" id="languageOutput">Detected Language: Waiting...</div>
|
| 102 |
-
<div class="transcription" id="transcriptionOutput">Transcription: Waiting...</div>
|
| 103 |
-
</div>
|
| 104 |
-
|
| 105 |
-
<style>
|
| 106 |
-
.transcription-container {
|
| 107 |
-
max-width: 600px;
|
| 108 |
-
margin: 20px auto;
|
| 109 |
-
padding: 20px;
|
| 110 |
-
background: #16213e;
|
| 111 |
-
border-radius: 10px;
|
| 112 |
-
box-shadow: 0 10px 20px rgba(0, 0, 0, 0.3);
|
| 113 |
-
color: #fff;
|
| 114 |
-
text-align: center;
|
| 115 |
-
}
|
| 116 |
-
.language, .transcription {
|
| 117 |
-
margin: 10px 0;
|
| 118 |
-
padding: 10px;
|
| 119 |
-
background: #0f172a;
|
| 120 |
-
border-radius: 5px;
|
| 121 |
-
}
|
| 122 |
-
</style>
|
| 123 |
-
|
| 124 |
-
<script>
|
| 125 |
-
setInterval(() => {
|
| 126 |
-
const languageOutput = document.querySelector('div[label="Detected Language"] textarea');
|
| 127 |
-
const transcriptionOutput = document.querySelector('div[label="Transcription"] textarea');
|
| 128 |
-
if (languageOutput && languageOutput.value) {
|
| 129 |
-
document.getElementById('languageOutput').textContent = `Detected Language: ${languageOutput.value}`;
|
| 130 |
-
}
|
| 131 |
-
if (transcriptionOutput && transcriptionOutput.value) {
|
| 132 |
-
document.getElementById('transcriptionOutput').textContent = `Transcription: ${transcriptionOutput.value}`;
|
| 133 |
-
}
|
| 134 |
-
}, 1000);
|
| 135 |
-
</script>
|
| 136 |
-
"""
|
| 137 |
-
|
| 138 |
-
# Gradio interface
|
| 139 |
-
with gr.Blocks() as demo:
|
| 140 |
-
gr.Markdown("# Audio Transcriptor")
|
| 141 |
-
gr.Markdown("Upload an audio file or record audio to transcribe the speech and detect the language. You can also interact with the bot via Telegram!")
|
| 142 |
-
|
| 143 |
-
with gr.Row():
|
| 144 |
-
audio_file = gr.Audio(sources=["upload"], type="filepath", label="Upload Audio")
|
| 145 |
-
audio_record = gr.Audio(sources=["microphone"], type="numpy", label="Record Audio")
|
| 146 |
-
|
| 147 |
-
with gr.Row():
|
| 148 |
-
language_output = gr.Textbox(label="Detected Language")
|
| 149 |
-
transcription_output = gr.Textbox(label="Transcription")
|
| 150 |
-
text_file_output = gr.File(label="Download Transcription as Text File")
|
| 151 |
-
|
| 152 |
-
# Add styled HTML section
|
| 153 |
-
gr.HTML(transcription_html)
|
| 154 |
-
|
| 155 |
-
with gr.Row():
|
| 156 |
-
submit = gr.Button("Transcribe")
|
| 157 |
-
clear = gr.Button("Clear")
|
| 158 |
-
|
| 159 |
-
submit.click(
|
| 160 |
-
fn=audio_transcriptor,
|
| 161 |
-
inputs=[audio_file, audio_record],
|
| 162 |
-
outputs=[language_output, transcription_output, text_file_output]
|
| 163 |
-
)
|
| 164 |
-
clear.click(
|
| 165 |
-
fn=lambda: (None, None),
|
| 166 |
-
inputs=[],
|
| 167 |
-
outputs=[audio_file, audio_record]
|
| 168 |
-
)
|
| 169 |
-
|
| 170 |
-
# Start the Telegram bot in the main thread
|
| 171 |
def run_telegram_bot():
|
| 172 |
if not TELEGRAM_BOT_TOKEN:
|
| 173 |
print("Telegram bot token not found. Please set TELEGRAM_BOT_TOKEN in the Space secrets.")
|
|
@@ -177,21 +30,12 @@ def run_telegram_bot():
|
|
| 177 |
|
| 178 |
# Add handlers
|
| 179 |
application.add_handler(CommandHandler("start", start))
|
| 180 |
-
application.add_handler(MessageHandler(filters.
|
| 181 |
|
| 182 |
# Start the bot in the main thread
|
| 183 |
print("Starting Telegram bot...")
|
| 184 |
asyncio.run(application.run_polling(allowed_updates=Update.ALL_TYPES))
|
| 185 |
|
| 186 |
-
#
|
| 187 |
-
def run_gradio():
|
| 188 |
-
demo.launch(ssr_mode=False)
|
| 189 |
-
|
| 190 |
-
# Launch Telegram bot in the main thread, Gradio in a background thread
|
| 191 |
if __name__ == "__main__":
|
| 192 |
-
import threading
|
| 193 |
-
# Start Gradio in a background thread
|
| 194 |
-
gradio_thread = threading.Thread(target=run_gradio)
|
| 195 |
-
gradio_thread.start()
|
| 196 |
-
# Run Telegram bot in the main thread
|
| 197 |
run_telegram_bot()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
import asyncio
|
| 3 |
from telegram import Update
|
| 4 |
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes
|
| 5 |
+
from transformers import pipeline
|
| 6 |
|
| 7 |
# Telegram bot token (to be set via Hugging Face Space secrets)
|
| 8 |
TELEGRAM_BOT_TOKEN = os.getenv("TELEGRAM_BOT_TOKEN")
|
| 9 |
|
| 10 |
+
# Load the AI model for text generation
|
| 11 |
+
generator = pipeline("text-generation", model="distilgpt2")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# Telegram bot handlers
|
| 14 |
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 15 |
+
await update.message.reply_text("Hello! I'm a chatbot powered by an AI model. Send me a message, and I'll respond!")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
| 18 |
+
user_message = update.message.text
|
| 19 |
+
# Generate a response using the AI model
|
| 20 |
+
response = generator(user_message, max_length=50, num_return_sequences=1, truncation=True)[0]['generated_text']
|
| 21 |
+
await update.message.reply_text(response)
|
| 22 |
|
| 23 |
+
# Run the Telegram bot
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
def run_telegram_bot():
|
| 25 |
if not TELEGRAM_BOT_TOKEN:
|
| 26 |
print("Telegram bot token not found. Please set TELEGRAM_BOT_TOKEN in the Space secrets.")
|
|
|
|
| 30 |
|
| 31 |
# Add handlers
|
| 32 |
application.add_handler(CommandHandler("start", start))
|
| 33 |
+
application.add_handler(MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message))
|
| 34 |
|
| 35 |
# Start the bot in the main thread
|
| 36 |
print("Starting Telegram bot...")
|
| 37 |
asyncio.run(application.run_polling(allowed_updates=Update.ALL_TYPES))
|
| 38 |
|
| 39 |
+
# Run the bot
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
run_telegram_bot()
|