Spaces:
Configuration error
Configuration error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
23 |
+
|
24 |
+
# Function to post data to an API and return response
|
25 |
+
def query(payload, API_URL, headers):
|
26 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
27 |
+
return response.content
|
28 |
+
|
29 |
+
# Function to generate images based on prompts using the Hugging Face API
|
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 |
+
|
36 |
+
}
|
37 |
+
API_URL = model_urls[model_choice]
|
38 |
+
|
39 |
+
headers = {"Authorization": f"Bearer {os.getenv('API_TOKEN')}"}
|
40 |
+
payload = {"inputs": prompt}
|
41 |
+
data = query(payload, API_URL, headers)
|
42 |
+
try:
|
43 |
+
# Load the image from byte data
|
44 |
+
image = Image.open(BytesIO(data))
|
45 |
+
# Resize the image
|
46 |
+
image = image.resize((400, 400))
|
47 |
+
# Convert the image object back to bytes for Gradio output
|
48 |
+
buf = BytesIO()
|
49 |
+
image.save(buf, format='PNG')
|
50 |
+
buf.seek(0)
|
51 |
+
return image
|
52 |
+
|
53 |
+
except Exception as e:
|
54 |
+
print("Error processing the image:", e)
|
55 |
+
return None # Return None or an appropriate error message/image
|
56 |
+
|
57 |
+
# Set up environment variable correctly
|
58 |
+
API_TOKEN = os.getenv("API_TOKEN")
|
59 |
+
|
60 |
+
# Styling with custom CSS
|
61 |
+
css = """
|
62 |
+
body {background-color: #f0f2f5;}
|
63 |
+
.gradio-app {background-color: #ffffff; border-radius: 12px; box-shadow: 0 0 12px rgba(0,0,0,0.1);}
|
64 |
+
button {color: white; background-color: #106BA3; border: none; border-radius: 5px;}
|
65 |
+
"""
|
66 |
+
|
67 |
+
# Define interface
|
68 |
+
title = "نموذج توليد الصور"
|
69 |
+
description = "اكتب وصف للصورة التي تود من النظام التوليدي انشاءها. على سبيل المثال: 'قطة ترتدي قبعة في مشهد شتوي'."
|
70 |
+
iface = gr.Interface(
|
71 |
+
fn=generate_image,
|
72 |
+
inputs=[
|
73 |
+
gr.components.Textbox(lines=2, placeholder="Enter the description of the image here..."),
|
74 |
+
gr.components.Dropdown(choices=["Stable Diffusion v1.5",], label="Choose Model", value='Stable Diffusion v1.5'),
|
75 |
+
gr.components.Checkbox(label="Translate The Text Before Generating Image", value=False)
|
76 |
+
],
|
77 |
+
outputs=gr.components.Image(),
|
78 |
+
title=title,
|
79 |
+
description=description,
|
80 |
+
theme="default",
|
81 |
+
css=css
|
82 |
+
)
|
83 |
+
# Launch the interface
|
84 |
+
iface.launch()
|