diginoron commited on
Commit
8e811a5
·
verified ·
1 Parent(s): 0966557

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -7
app.py CHANGED
@@ -3,16 +3,25 @@ import requests
3
  from PIL import Image
4
  from io import BytesIO
5
  import os
 
6
 
7
- # آدرس API مدل Stable Diffusion از Hugging Face
8
  API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2"
9
  headers = {
10
- "Authorization": f"Bearer {os.environ.get('HF_TOKEN')}" # توی Secrets ست میشه
11
  }
12
 
13
  def generate_image(prompt):
 
 
 
 
 
 
 
 
14
  payload = {
15
- "inputs": prompt,
16
  "options": {"wait_for_model": True}
17
  }
18
 
@@ -22,14 +31,15 @@ def generate_image(prompt):
22
  image = Image.open(BytesIO(response.content))
23
  return image
24
  except Exception as e:
25
- return f"❌ Error: {str(e)}\n\nRaw content:\n{response.content[:500]}"
26
 
 
27
  iface = gr.Interface(
28
  fn=generate_image,
29
- inputs=gr.Textbox(label="Prompt", placeholder="e.g. A cat sitting in a cardboard box, black and white sketch..."),
30
  outputs="image",
31
- title="🎨 Stable Diffusion Image Generator",
32
- description="Generate images from text prompts using Stable Diffusion via Hugging Face Inference API."
33
  )
34
 
35
  iface.launch()
 
3
  from PIL import Image
4
  from io import BytesIO
5
  import os
6
+ from deep_translator import GoogleTranslator # برای ترجمه
7
 
8
+ # آدرس API مدل Stable Diffusion در Hugging Face
9
  API_URL = "https://api-inference.huggingface.co/models/stabilityai/stable-diffusion-2"
10
  headers = {
11
+ "Authorization": f"Bearer {os.environ.get('HF_TOKEN')}"
12
  }
13
 
14
  def generate_image(prompt):
15
+ try:
16
+ # ترجمه خودکار پرامپت فارسی به انگلیسی
17
+ translated_prompt = GoogleTranslator(source='auto', target='en').translate(prompt)
18
+ print(f"🈯 Prompt ترجمه‌شده: {translated_prompt}")
19
+ except Exception as e:
20
+ return f"❌ خطا در ترجمه پرامپت: {str(e)}"
21
+
22
+ # ارسال درخواست به API
23
  payload = {
24
+ "inputs": translated_prompt,
25
  "options": {"wait_for_model": True}
26
  }
27
 
 
31
  image = Image.open(BytesIO(response.content))
32
  return image
33
  except Exception as e:
34
+ return f"❌ خطا در پردازش تصویر: {str(e)}\n\n🧾 پاسخ:\n{response.content[:500]}"
35
 
36
+ # رابط Gradio
37
  iface = gr.Interface(
38
  fn=generate_image,
39
+ inputs=gr.Textbox(label="پرامپت فارسی", placeholder="مثلاً: یک گربه داخل جعبه نشسته است..."),
40
  outputs="image",
41
+ title="🎨 تولید تصویر با پرامپت فارسی + Stable Diffusion",
42
+ description="پرامپت فارسی بده، ترجمه می‌کنیم و با مدل Stable Diffusion تصویر تولید می‌کنیم. (با استفاده از Hugging Face Inference API)"
43
  )
44
 
45
  iface.launch()