majedk01 commited on
Commit
5565413
1 Parent(s): 3181370

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -7
app.py CHANGED
@@ -1,17 +1,22 @@
 
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,13 +30,15 @@ def query(payload, API_URL, headers):
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,6 +50,10 @@ def generate_image(prompt, model_choice, translate=False):
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,11 +76,11 @@ description = "اكتب وصف للصورة التي تود من النظام ا
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",
 
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
  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
  }
43
  API_URL = model_urls[model_choice]
44
 
 
50
  image = Image.open(BytesIO(data))
51
  # Resize the image
52
  image = image.resize((400, 400))
53
+ # Convert the image object back to bytes for Gradio output
54
+ buf = BytesIO()
55
+ image.save(buf, format='PNG')
56
+ buf.seek(0)
57
  return image
58
 
59
  except Exception as e:
 
76
  iface = gr.Interface(
77
  fn=generate_image,
78
  inputs=[
79
+ gr.components.Textbox(lines=2, placeholder="Enter the description of the image here..."),
80
+ 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'),
81
+
82
  ],
83
+ outputs=gr.components.Image(),
84
  title=title,
85
  description=description,
86
  theme="default",