aiqcamp commited on
Commit
ba6e005
Β·
verified Β·
1 Parent(s): aeeefc6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -5
app.py CHANGED
@@ -142,9 +142,23 @@ def get_model() -> tuple[MMAudio, FeaturesUtils, SequenceConfig]:
142
 
143
  net, feature_utils, seq_cfg = get_model()
144
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
  def translate_prompt(text):
146
  try:
147
  if text and any(ord(char) >= 0x3131 and ord(char) <= 0xD7A3 for char in text):
 
148
  with torch.no_grad():
149
  translation = translator(text)[0]['translation_text']
150
  return translation
@@ -153,6 +167,22 @@ def translate_prompt(text):
153
  logging.error(f"Translation error: {e}")
154
  return text
155
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156
  def search_pixabay_videos(query, api_key):
157
  try:
158
  base_url = "https://pixabay.com/api/videos/"
@@ -171,11 +201,6 @@ def search_pixabay_videos(query, api_key):
171
  logging.error(f"Pixabay API error: {e}")
172
  return []
173
 
174
- @torch.no_grad()
175
- def search_videos(query):
176
- with torch.cuda.device("cpu"):
177
- query = translate_prompt(query)
178
- return search_pixabay_videos(query, PIXABAY_API_KEY)
179
 
180
  @spaces.GPU
181
  @torch.inference_mode()
 
142
 
143
  net, feature_utils, seq_cfg = get_model()
144
 
145
+
146
+ # search_videos ν•¨μˆ˜ μˆ˜μ •
147
+ @torch.no_grad()
148
+ def search_videos(query):
149
+ try:
150
+ # CPUμ—μ„œ λ²ˆμ—­ μ‹€ν–‰
151
+ query = translate_prompt(query)
152
+ return search_pixabay_videos(query, PIXABAY_API_KEY)
153
+ except Exception as e:
154
+ logging.error(f"Video search error: {e}")
155
+ return []
156
+
157
+ # translate_prompt ν•¨μˆ˜λ„ μˆ˜μ •
158
  def translate_prompt(text):
159
  try:
160
  if text and any(ord(char) >= 0x3131 and ord(char) <= 0xD7A3 for char in text):
161
+ # CPUμ—μ„œ λ²ˆμ—­ μ‹€ν–‰
162
  with torch.no_grad():
163
  translation = translator(text)[0]['translation_text']
164
  return translation
 
167
  logging.error(f"Translation error: {e}")
168
  return text
169
 
170
+ # λ””λ°”μ΄μŠ€ μ„€μ • λΆ€λΆ„ μˆ˜μ •
171
+ if torch.cuda.is_available():
172
+ device = torch.device("cuda")
173
+ torch.backends.cuda.matmul.allow_tf32 = True
174
+ torch.backends.cudnn.allow_tf32 = True
175
+ torch.backends.cudnn.benchmark = True
176
+ else:
177
+ device = torch.device("cpu")
178
+
179
+ # λ²ˆμ—­κΈ° μ„€μ • μˆ˜μ •
180
+ translator = pipeline("translation",
181
+ model="Helsinki-NLP/opus-mt-ko-en",
182
+ device="cpu") # λͺ…μ‹œμ μœΌλ‘œ CPU μ§€μ •
183
+
184
+
185
+
186
  def search_pixabay_videos(query, api_key):
187
  try:
188
  base_url = "https://pixabay.com/api/videos/"
 
201
  logging.error(f"Pixabay API error: {e}")
202
  return []
203
 
 
 
 
 
 
204
 
205
  @spaces.GPU
206
  @torch.inference_mode()