ZoyaRabail commited on
Commit
6fa4f33
·
verified ·
1 Parent(s): b3a50e8

Rename translation/app.py to stt_app.py

Browse files
Files changed (2) hide show
  1. stt_app.py +24 -0
  2. translation/app.py +0 -121
stt_app.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+
4
+ # Load Whisper model
5
+ stt_pipeline = pipeline("automatic-speech-recognition", model="openai/whisper-small")
6
+
7
+ def transcribe(audio):
8
+ if audio is None:
9
+ return "Please record some audio."
10
+ result = stt_pipeline(audio)
11
+ return result["text"]
12
+
13
+ with gr.Blocks() as demo:
14
+ gr.Markdown("# 🎙️ Speech to Text Converter")
15
+ gr.Markdown("Supports **English, Spanish, French, German, Portuguese, Italian, Russian, Chinese**")
16
+
17
+ with gr.Row():
18
+ with gr.Column():
19
+ audio_input = gr.Audio(sources=["microphone"], type="filepath", label="🎤 Record Speech")
20
+ transcribe_btn = gr.Button("Transcribe")
21
+ with gr.Column():
22
+ output_text = gr.Textbox(label="📝 Transcribed Text", lines=8)
23
+
24
+ transcribe_btn.click(fn=transcribe, inputs=audio_input, outputs=output_text)
translation/app.py DELETED
@@ -1,121 +0,0 @@
1
- import os
2
- import json
3
- import requests
4
- import gradio as gr
5
- from langdetect import detect, LangDetectException
6
-
7
- # Hugging Face Transformers
8
- from transformers import M2M100ForConditionalGeneration, M2M100Tokenizer
9
-
10
- # Groq SDK
11
- try:
12
- from groq import Groq
13
- except Exception:
14
- Groq = None
15
-
16
- # Config
17
- GROQ_API_KEY = os.getenv("GROQ_API_KEY")
18
- GROQ_MODEL = os.getenv("GROQ_MODEL", "mixtral-8x7b-32768")
19
- HUGGINGFACE_TOKEN = os.getenv("HUGGINGFACE_TOKEN")
20
-
21
- # Init Groq
22
- groq_client = None
23
- if GROQ_API_KEY and Groq is not None:
24
- try:
25
- groq_client = Groq(api_key=GROQ_API_KEY)
26
- except Exception as e:
27
- print("Groq client init failed:", repr(e))
28
-
29
- # Universal translation model
30
- m2m_model_name = "facebook/m2m100_418M"
31
- m2m_tokenizer = M2M100Tokenizer.from_pretrained(m2m_model_name)
32
- m2m_model = M2M100ForConditionalGeneration.from_pretrained(m2m_model_name)
33
-
34
- # UI mapping
35
- LANG_UI_TO_CODE = {"English": "en", "Spanish": "es", "French": "fr"}
36
-
37
- SYSTEM_PROMPT = """
38
- You are a multilingual translation assistant.
39
- Task:
40
- 1. Detect the input language automatically.
41
- 2. Translate into the requested target language.
42
- 3. Preserve meaning, tone, and formatting.
43
- 4. Keep numbers, symbols, names, and special characters unchanged.
44
- 5. If the input is already in the target language, return it unchanged.
45
- """
46
-
47
- def call_groq(user_text, target_lang_ui):
48
- if not groq_client:
49
- raise RuntimeError("Groq client not configured")
50
- messages = [
51
- {"role": "system", "content": SYSTEM_PROMPT},
52
- {"role": "user", "content": f"Target language: {target_lang_ui}\n\n{user_text}"},
53
- ]
54
- chat = groq_client.chat.completions.create(
55
- model=GROQ_MODEL,
56
- messages=messages,
57
- temperature=0,
58
- max_tokens=2048,
59
- )
60
- try:
61
- return chat.choices[0].message.content.strip()
62
- except Exception:
63
- try:
64
- return chat["choices"][0]["message"]["content"].strip()
65
- except Exception as e:
66
- print("Unexpected Groq response:", repr(e))
67
- raise
68
-
69
- def call_m2m(user_text, target_code):
70
- try:
71
- src_code = detect(user_text)
72
- except LangDetectException:
73
- src_code = "en" # fallback
74
-
75
- # if already target language → return as-is
76
- if src_code == target_code:
77
- return user_text
78
-
79
- m2m_tokenizer.src_lang = src_code
80
- encoded = m2m_tokenizer(user_text, return_tensors="pt")
81
- generated = m2m_model.generate(
82
- **encoded, forced_bos_token_id=m2m_tokenizer.get_lang_id(target_code)
83
- )
84
- return m2m_tokenizer.decode(generated[0], skip_special_tokens=True)
85
-
86
- def translate_text(user_text, target_lang_ui):
87
- user_text = (user_text or "").strip()
88
- if not user_text:
89
- return "⚠️ Please enter some text to translate."
90
- target_code = LANG_UI_TO_CODE.get(target_lang_ui, "en")
91
-
92
- # Try Groq first
93
- try:
94
- if groq_client:
95
- out = call_groq(user_text, target_lang_ui)
96
- if out:
97
- return out
98
- except Exception as e:
99
- print("Groq call failed:", repr(e))
100
-
101
- # Fallback → M2M100 universal translator
102
- try:
103
- return call_m2m(user_text, target_code)
104
- except Exception as e:
105
- print("M2M100 translation failed:", repr(e))
106
- return "❌ Translation failed. Check logs."
107
-
108
- # ----------------- Gradio UI -----------------
109
- with gr.Blocks() as demo:
110
- gr.Markdown("## 🌐 Hackathon Translator (Universal)")
111
-
112
- with gr.Row():
113
- txt = gr.Textbox(label="Enter your text", lines=6, placeholder="Type or paste text here...")
114
- tgt = gr.Dropdown(choices=["English","Spanish","French"], value="English", label="Target Language")
115
-
116
- out = gr.Textbox(label="Translated Output", lines=6)
117
- btn = gr.Button("Translate")
118
- btn.click(fn=translate_text, inputs=[txt, tgt], outputs=[out])
119
-
120
- if __name__ == "__main__":
121
- demo.launch()