Hamed744 commited on
Commit
7bf5b22
·
verified ·
1 Parent(s): f230e42

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -13
app.py CHANGED
@@ -11,18 +11,24 @@ from typing import Optional
11
  # برنامه اصلی FastAPI
12
  # -------------------------
13
  app = FastAPI(
14
- title="Veo 3.1 Fast API",
15
- description="API for generating videos from text or images using Hugging Face Inference Providers."
16
  )
17
 
18
  # -------------------------
19
- # توابع کمکی (برگرفته از کد اصلی شما)
20
  # -------------------------
21
 
22
  def _client_from_token(token: Optional[str]) -> InferenceClient:
23
- """ایجاد یک کلاینت برای ارتباط با API هاگینگ فیس"""
24
- if not token or not token.strip().startswith("hf_"):
25
- raise HTTPException(status_code=400, detail="لطفاً توکن معتبر هاگینگ فیس خود را وارد کنید (باید با hf_ شروع شود).")
 
 
 
 
 
 
26
  return InferenceClient(
27
  provider="fal-ai",
28
  api_key=token,
@@ -34,12 +40,12 @@ def handle_api_exceptions(e: Exception):
34
  # خطای دسترسی یا کمبود اعتبار
35
  raise HTTPException(
36
  status_code=403,
37
- detail="Access denied by provider (403). Make sure your HF account has credits/permission for this provider and model."
38
  )
39
  # خطاهای دیگر
40
  raise HTTPException(
41
  status_code=500,
42
- detail=f"Generation failed. If this keeps happening, check your provider quota or try again later. Error: {str(e)}"
43
  )
44
 
45
  # -------------------------
@@ -58,7 +64,7 @@ async def api_text_to_video(
58
  ):
59
  """Endpoint برای تبدیل متن به ویدیو"""
60
  if not prompt or not prompt.strip():
61
- raise HTTPException(status_code=400, detail="Please enter a text prompt.")
62
 
63
  try:
64
  client = _client_from_token(token)
@@ -66,11 +72,10 @@ async def api_text_to_video(
66
  prompt,
67
  model="akhaliq/veo3.1-fast",
68
  )
69
- # بازگرداندن ویدیو به صورت stream
70
  return StreamingResponse(io.BytesIO(video_bytes), media_type="video/mp4")
71
 
72
  except HTTPException as e:
73
- raise e # ارسال مجدد خطاهای شناخته شده
74
  except Exception as e:
75
  handle_api_exceptions(e)
76
 
@@ -83,9 +88,9 @@ async def api_image_to_video(
83
  ):
84
  """Endpoint برای تبدیل تصویر به ویدیو"""
85
  if not prompt or not prompt.strip():
86
- raise HTTPException(status_code=400, detail="Please enter a motion prompt.")
87
  if not image.content_type.startswith("image/"):
88
- raise HTTPException(status_code=400, detail="Invalid file type. Please upload an image.")
89
 
90
  try:
91
  input_image_bytes = await image.read()
 
11
  # برنامه اصلی FastAPI
12
  # -------------------------
13
  app = FastAPI(
14
+ title="AI Video Studio API",
15
+ description="API for generating videos from text or images."
16
  )
17
 
18
  # -------------------------
19
+ # توابع کمکی
20
  # -------------------------
21
 
22
  def _client_from_token(token: Optional[str]) -> InferenceClient:
23
+ """ایجاد یک کلاینت برای ارتباط با API"""
24
+ # تغییر کلیدی در اینجا: حذف بررسی "hf_" و تغییر پیام خطا
25
+ if not token or not token.strip():
26
+ raise HTTPException(status_code=400, detail="رمز ورود نامعتبر است یا دسترسی لازم را ندارد.")
27
+
28
+ # اینجا می‌توانید یک بررسی ساده‌تر انجام دهید، مثلاً طول توکن
29
+ if len(token.strip()) < 10: # یک بررسی ساده برای جلوگیری از ورودی‌های خیلی کوتاه
30
+ raise HTTPException(status_code=400, detail="رمز ورود نامعتبر است یا دسترسی لازم را ندارد.")
31
+
32
  return InferenceClient(
33
  provider="fal-ai",
34
  api_key=token,
 
40
  # خطای دسترسی یا کمبود اعتبار
41
  raise HTTPException(
42
  status_code=403,
43
+ detail="دسترسی رد شد (403). مطمئن شوید رمز شما معتبر بوده و اعتبار کافی برای استفاده از سرویس را دارید."
44
  )
45
  # خطاهای دیگر
46
  raise HTTPException(
47
  status_code=500,
48
+ detail=f"ساخت ویدیو با خطا مواجه شد. لطفاً اعتبار حساب خود را بررسی کرده یا بعداً دوباره تلاش کنید."
49
  )
50
 
51
  # -------------------------
 
64
  ):
65
  """Endpoint برای تبدیل متن به ویدیو"""
66
  if not prompt or not prompt.strip():
67
+ raise HTTPException(status_code=400, detail="لطفاً یک دستور متنی وارد کنید.")
68
 
69
  try:
70
  client = _client_from_token(token)
 
72
  prompt,
73
  model="akhaliq/veo3.1-fast",
74
  )
 
75
  return StreamingResponse(io.BytesIO(video_bytes), media_type="video/mp4")
76
 
77
  except HTTPException as e:
78
+ raise e
79
  except Exception as e:
80
  handle_api_exceptions(e)
81
 
 
88
  ):
89
  """Endpoint برای تبدیل تصویر به ویدیو"""
90
  if not prompt or not prompt.strip():
91
+ raise HTTPException(status_code=400, detail="لطفاً یک دستور حرکت وارد کنید.")
92
  if not image.content_type.startswith("image/"):
93
+ raise HTTPException(status_code=400, detail="فایل نامعتبر است. لطفاً یک تصویر آپلود کنید.")
94
 
95
  try:
96
  input_image_bytes = await image.read()