LG3370 commited on
Commit
5f430e2
·
verified ·
1 Parent(s): cbf57f1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -38
app.py CHANGED
@@ -2,53 +2,53 @@ import gradio as gr
2
  import spaces
3
  import torch
4
  from transformers import pipeline
5
- import time
6
 
7
  MODEL_ID = "collabora/whisper-large-v2-hindi"
8
 
9
- @spaces.GPU(duration=120)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  def transcribe_hindi(audio):
11
  if audio is None:
12
  return "कृपया ऑडियो प्रदान करें।", None
13
 
14
- max_retries = 3
15
- delay = 5 # शुरुआती डिले ५ सेकंड
16
-
17
- for attempt in range(max_retries):
18
- try:
19
- # पाइपलाइन निर्माण का प्रयास
20
- asr_pipe = pipeline(
21
- "automatic-speech-recognition",
22
- model=MODEL_ID,
23
- chunk_length_s=30,
24
- device="cuda",
25
- torch_dtype=torch.float16,
26
- batch_size=8
27
- )
28
 
29
- # यदि पाइपलाइन सफलतापूर्वक बन गई, तो ट्रांसक्रिप्शन करें
30
- result = r = asr_pipe(audio, generate_kwargs={"language": "hindi"})
31
- text_output = result["text"].strip()
32
-
33
- file_path = "transcription.txt"
34
- with open(file_path, "w", encoding="utf-8") as f:
35
- f.write(text_output)
36
-
37
- return text_output, file_path
38
 
39
- except RuntimeError as e:
40
- # यदि एरर NVIDIA ड्राइवर या CUDA से संबंधित है और प्रयास बाकी हैं
41
- if ("NVIDIA driver" in str(e) or "CUDA" in str(e)) and (attempt < max_retries - 1):
42
- print(f"प्रयास {attempt + 1} विफल। {delay} सेकंड में पुनः प्रयास किया जा रहा है...")
43
- time.sleep(delay)
44
- delay *= 2 # एक्सपोनेंशियल डिले (५, १०, २० सेकंड)
45
- continue
46
- else:
47
- # यदि सारे प्रयास विफल हो जाएं या कोई और एरर हो
48
- if "NVIDIA driver" in str(e) or "CUDA" in str(e):
49
- return "त्रुटि: Hugging Face ZeroGPU पर अत्यधिक लोड है और ३ स्वचालित प्रयासों के बाद भी ड्राइवर कनेक्ट नहीं हो सका। कृपया कुछ समय पश्चात प्रयास करें।", None
50
- else:
51
- return f"एक अप्रत्याशित त्रुटि आई: {str(e)}", None
52
 
53
  custom_css = """
54
  footer {visibility: hidden}
 
2
  import spaces
3
  import torch
4
  from transformers import pipeline
 
5
 
6
  MODEL_ID = "collabora/whisper-large-v2-hindi"
7
 
8
+ print("काव्यात्मक सृजन के लिए मॉडल को ग्लोबल मेमोरी में लोड किया जा रहा है...")
9
+
10
+ # मॉडल को ग्लोबल स्पेस में केवल एक बार लोड कर रहे हैं ताकि बार-बार लोडिंग का समय बचे
11
+ # 'device="cpu"' रखने से यह ऐप स्टार्ट होते समय बिना GPU के सुरक्षित लोड हो जाएगा
12
+ asr_pipe = pipeline(
13
+ "automatic-speech-recognition",
14
+ model=MODEL_ID,
15
+ chunk_length_s=30,
16
+ device="cpu", # शुरुआत में CPU पर रहेगा
17
+ dtype=torch.float16 # पुरानी चेतावनी को दूर करने के लिए 'torch_dtype' की जगह 'dtype'
18
+ )
19
+
20
+ print("मॉडल सफलतापूर्वक रैम (RAM) में लोड हो चुका है!")
21
+
22
+ # अब केवल ट्रांसक्रिप्शन की मुख्य प्रक्रिया को GPU सौंपेंगे
23
+ @spaces.GPU(duration=60)
24
  def transcribe_hindi(audio):
25
  if audio is None:
26
  return "कृपया ऑडियो प्रदान करें।", None
27
 
28
+ try:
29
+ # ग्लोबल पाइपलाइन के मॉडल को तुरंत GPU (cuda) पर ट्रांसफर करें
30
+ asr_pipe.model.to("cuda")
31
+
32
+ # प्रोसेसिंग को तेज़ बनाने के लिए बैचिंग (अंदरूनी तौर पर)
33
+ result = asr_pipe(audio, batch_size=8, generate_kwargs={"language": "hindi"})
34
+ text_output = result["text"].strip()
35
+
36
+ # काम पूरा होते ही मॉडल को वापस CPU पर भेजें ताकि ZeroGPU फ्री हो सके
37
+ asr_pipe.model.to("cpu")
38
+
39
+ file_path = "transcription.txt"
40
+ with open(file_path, "w", encoding="utf-8") as f:
41
+ f.write(text_output)
42
 
43
+ return text_output, file_path
 
 
 
 
 
 
 
 
44
 
45
+ except Exception as e:
46
+ # किसी भी स्थिति में मॉडल को सेफ स्टेट (CPU) पर लाना सुरक्षित रहता है
47
+ try:
48
+ asr_pipe.model.to("cpu")
49
+ except:
50
+ pass
51
+ return f"प्रक्रिया में कुछ व्यवधान आया: {str(e)}। कृपया पुनः प्रयास करें।", None
 
 
 
 
 
 
52
 
53
  custom_css = """
54
  footer {visibility: hidden}