majedk01 commited on
Commit
3181370
1 Parent(s): 66e37f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -17
app.py CHANGED
@@ -1,22 +1,17 @@
1
- ## app.py:
2
  import torch
3
  import gradio as gr
4
- from diffusers import StableDiffusionPipeline
5
  import requests
6
  from io import BytesIO
7
- import os
8
  from PIL import Image
9
-
10
-
11
 
12
  def translate_text(text, target_language='en'):
13
  API_URL = "https://api-inference.huggingface.co/models/Helsinki-NLP/opus-mt-ar-en"
14
  headers = {"Authorization": f"Bearer {os.getenv('API_TOKEN')}"}
15
- response = requests.post(API_URL, headers=headers, json=text)
16
 
17
  if response.status_code == 200:
18
  return response.json()[0]['translation_text']
19
-
20
  else:
21
  print("Failed to translate text:", response.text)
22
  return text # Return the original text if translation fails
@@ -30,14 +25,13 @@ def query(payload, API_URL, headers):
30
  def generate_image(prompt, model_choice, translate=False):
31
  if translate:
32
  prompt = translate_text(prompt, target_language='en') # Assuming you want to translate to English
 
33
  model_urls = {
34
  "Stable Diffusion v1.5": "https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5",
35
  "dalle-3-xl-v2": "https://api-inference.huggingface.co/models/ehristoforu/dalle-3-xl-v2",
36
  "midjourney-v6": "https://api-inference.huggingface.co/models/Kvikontent/midjourney-v6",
37
  "openjourney-v4": "https://api-inference.huggingface.co/models/prompthero/openjourney-v4",
38
  "LCM_Dreamshaper_v7": "https://api-inference.huggingface.co/models/SimianLuo/LCM_Dreamshaper_v7",
39
-
40
-
41
  }
42
  API_URL = model_urls[model_choice]
43
 
@@ -49,10 +43,6 @@ def generate_image(prompt, model_choice, translate=False):
49
  image = Image.open(BytesIO(data))
50
  # Resize the image
51
  image = image.resize((400, 400))
52
- # Convert the image object back to bytes for Gradio output
53
- buf = BytesIO()
54
- image.save(buf, format='PNG')
55
- buf.seek(0)
56
  return image
57
 
58
  except Exception as e:
@@ -75,11 +65,11 @@ description = "اكتب وصف للصورة التي تود من النظام ا
75
  iface = gr.Interface(
76
  fn=generate_image,
77
  inputs=[
78
- gr.components.Textbox(lines=2, placeholder="Enter the description of the image here..."),
79
- gr.components.Dropdown(choices=["Stable Diffusion v1.5","dalle-3-xl-v2","midjourney-v6","openjourney-v4","LCM_Dreamshaper_v7",], label="Choose Model", value='Stable Diffusion v1.5'),
80
- gr.components.Checkbox(label="Translate The Text Before Generating Image", value=False)
81
  ],
82
- outputs=gr.components.Image(),
83
  title=title,
84
  description=description,
85
  theme="default",
 
 
1
  import torch
2
  import gradio as gr
 
3
  import requests
4
  from io import BytesIO
 
5
  from PIL import Image
6
+ import os
 
7
 
8
  def translate_text(text, target_language='en'):
9
  API_URL = "https://api-inference.huggingface.co/models/Helsinki-NLP/opus-mt-ar-en"
10
  headers = {"Authorization": f"Bearer {os.getenv('API_TOKEN')}"}
11
+ response = requests.post(API_URL, headers=headers, json={"inputs": text})
12
 
13
  if response.status_code == 200:
14
  return response.json()[0]['translation_text']
 
15
  else:
16
  print("Failed to translate text:", response.text)
17
  return text # Return the original text if translation fails
 
25
  def generate_image(prompt, model_choice, translate=False):
26
  if translate:
27
  prompt = translate_text(prompt, target_language='en') # Assuming you want to translate to English
28
+
29
  model_urls = {
30
  "Stable Diffusion v1.5": "https://api-inference.huggingface.co/models/runwayml/stable-diffusion-v1-5",
31
  "dalle-3-xl-v2": "https://api-inference.huggingface.co/models/ehristoforu/dalle-3-xl-v2",
32
  "midjourney-v6": "https://api-inference.huggingface.co/models/Kvikontent/midjourney-v6",
33
  "openjourney-v4": "https://api-inference.huggingface.co/models/prompthero/openjourney-v4",
34
  "LCM_Dreamshaper_v7": "https://api-inference.huggingface.co/models/SimianLuo/LCM_Dreamshaper_v7",
 
 
35
  }
36
  API_URL = model_urls[model_choice]
37
 
 
43
  image = Image.open(BytesIO(data))
44
  # Resize the image
45
  image = image.resize((400, 400))
 
 
 
 
46
  return image
47
 
48
  except Exception as e:
 
65
  iface = gr.Interface(
66
  fn=generate_image,
67
  inputs=[
68
+ gr.Textbox(lines=2, placeholder="Enter the description of the image here..."),
69
+ gr.Dropdown(choices=["Stable Diffusion v1.5","dalle-3-xl-v2","midjourney-v6","openjourney-v4","LCM_Dreamshaper_v7"], label="Choose Model", value='Stable Diffusion v1.5'),
70
+ gr.Checkbox(label="Translate The Text Before Generating Image", value=False)
71
  ],
72
+ outputs=gr.Image(),
73
  title=title,
74
  description=description,
75
  theme="default",