archivartaunik commited on
Commit
4ba47fd
·
verified ·
1 Parent(s): 89be3e8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -13
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import os
2
  import gradio as gr
3
- import google.genai as genai
 
4
  import mimetypes
5
  from pydub import AudioSegment
6
 
@@ -10,13 +11,8 @@ MODEL_NAME_TH = os.getenv("modTH") # Мадэль для транскрыпцы
10
  MODEL_NAME = os.getenv("mod") # Мадэль для выпраўлення фармату і перакладу
11
  PROMPT_TRANSCRIBE = os.getenv("p") # Промпт для транскрыпцыі
12
 
13
- # Канфігурацыя API ключа
14
- genai.configure(api_key=GEMINI_API_KEY)
15
-
16
- # Стварэнне мадэляў
17
- model_transcribe = genai.GenerativeModel(MODEL_NAME_TH)
18
- model_general = genai.GenerativeModel(MODEL_NAME)
19
-
20
 
21
  def transcribe_audio(audio_file: str) -> str:
22
  """Транскрыбуе аўдыяфайл з дапамогай Google GenAI."""
@@ -31,14 +27,20 @@ def transcribe_audio(audio_file: str) -> str:
31
  with open(audio_file, "rb") as f:
32
  audio_data = f.read()
33
 
34
- # Стварэнне кантэнта для перадачы ў API
35
- content = [
36
  {"text": PROMPT_TRANSCRIBE},
37
  {"mime_type": mime_type, "data": audio_data},
38
  ]
39
 
40
  # Выклік API
41
- response = model_transcribe.generate_content(content)
 
 
 
 
 
 
42
 
43
  # Атрыманне адказу
44
  return response.text.strip()
@@ -57,7 +59,13 @@ def fix_subtitles_format(transcript: str) -> str:
57
  f"{transcript}"
58
  )
59
  try:
60
- response_fix = model_general.generate_content(prompt_fix)
 
 
 
 
 
 
61
  return response_fix.text.strip()
62
  except Exception as e:
63
  print(f"Памылка пры выпраўленні субцітраў: {e}")
@@ -131,7 +139,13 @@ def translate_transcript(transcript: str, target_language: str) -> tuple[str, st
131
  "Астатняе пакінь як ёсць.\nТэкст:\n{transcript}"
132
  )
133
  try:
134
- response = model_general.generate_content(prompt_text)
 
 
 
 
 
 
135
  translated = response.text.strip()
136
  translated_srt_filename = "translated_subtitles.srt"
137
  return create_srt(translated, translated_srt_filename)
 
1
  import os
2
  import gradio as gr
3
+ from google import genai
4
+ from google.genai import types
5
  import mimetypes
6
  from pydub import AudioSegment
7
 
 
11
  MODEL_NAME = os.getenv("mod") # Мадэль для выпраўлення фармату і перакладу
12
  PROMPT_TRANSCRIBE = os.getenv("p") # Промпт для транскрыпцыі
13
 
14
+ # Стварэнне кліента
15
+ client = genai.Client(api_key=GEMINI_API_KEY)
 
 
 
 
 
16
 
17
  def transcribe_audio(audio_file: str) -> str:
18
  """Транскрыбуе аўдыяфайл з дапамогай Google GenAI."""
 
27
  with open(audio_file, "rb") as f:
28
  audio_data = f.read()
29
 
30
+ # Стварэнне contents для перадачы ў API
31
+ contents = [
32
  {"text": PROMPT_TRANSCRIBE},
33
  {"mime_type": mime_type, "data": audio_data},
34
  ]
35
 
36
  # Выклік API
37
+ response = client.models.generate_content(
38
+ model=MODEL_NAME_TH,
39
+ contents=contents,
40
+ config=types.GenerateContentConfig(
41
+ temperature=0.2
42
+ )
43
+ )
44
 
45
  # Атрыманне адказу
46
  return response.text.strip()
 
59
  f"{transcript}"
60
  )
61
  try:
62
+ response_fix = client.models.generate_content(
63
+ model=MODEL_NAME,
64
+ contents=[{"text": prompt_fix}],
65
+ config=types.GenerateContentConfig(
66
+ temperature=0.2
67
+ )
68
+ )
69
  return response_fix.text.strip()
70
  except Exception as e:
71
  print(f"Памылка пры выпраўленні субцітраў: {e}")
 
139
  "Астатняе пакінь як ёсць.\nТэкст:\n{transcript}"
140
  )
141
  try:
142
+ response = client.models.generate_content(
143
+ model=MODEL_NAME,
144
+ contents=[{"text": prompt_text}],
145
+ config=types.GenerateContentConfig(
146
+ temperature=0.2
147
+ )
148
+ )
149
  translated = response.text.strip()
150
  translated_srt_filename = "translated_subtitles.srt"
151
  return create_srt(translated, translated_srt_filename)