Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,11 +5,14 @@ from dotenv import load_dotenv # Chỉ dùng khi phát triển cục bộ
|
|
| 5 |
import io
|
| 6 |
from PIL import Image
|
| 7 |
from mtranslate import translate # Import thư viện mtranslate
|
|
|
|
| 8 |
|
|
|
|
| 9 |
load_dotenv()
|
| 10 |
|
|
|
|
| 11 |
api_key = os.getenv("HF_API_KEY")
|
| 12 |
-
image_api_url =
|
| 13 |
headers = {"Authorization": f"Bearer {api_key}"}
|
| 14 |
|
| 15 |
# Hàm gọi API Hugging Face để tạo hình ảnh
|
|
@@ -17,6 +20,7 @@ def query(payload):
|
|
| 17 |
response = requests.post(image_api_url, headers=headers, json=payload)
|
| 18 |
return response.content
|
| 19 |
|
|
|
|
| 20 |
def translate_to_english(text):
|
| 21 |
return translate(text, "en", "vi") # Dịch từ tiếng Việt sang tiếng Anh
|
| 22 |
|
|
@@ -24,18 +28,23 @@ def translate_to_english(text):
|
|
| 24 |
def generate_image(prompt):
|
| 25 |
# Dịch prompt từ tiếng Việt sang tiếng Anh
|
| 26 |
prompt_en = translate_to_english(prompt)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
# Gửi prompt đã dịch vào API tạo hình ảnh
|
| 28 |
-
image_bytes = query({"inputs":
|
| 29 |
# Mở hình ảnh từ byte dữ liệu
|
| 30 |
image = Image.open(io.BytesIO(image_bytes))
|
| 31 |
return image
|
| 32 |
|
|
|
|
| 33 |
iface = gr.Interface(
|
| 34 |
fn=generate_image,
|
| 35 |
inputs="text",
|
| 36 |
outputs="image",
|
| 37 |
-
title="Image Generator
|
| 38 |
-
description="Nhập mô tả của bạn để tạo hình ảnh
|
| 39 |
)
|
| 40 |
|
| 41 |
# Khởi chạy ứng dụng
|
|
|
|
| 5 |
import io
|
| 6 |
from PIL import Image
|
| 7 |
from mtranslate import translate # Import thư viện mtranslate
|
| 8 |
+
import random # Thư viện để tạo yếu tố ngẫu nhiên
|
| 9 |
|
| 10 |
+
# Tải biến môi trường từ file .env nếu có (chỉ dùng khi phát triển cục bộ)
|
| 11 |
load_dotenv()
|
| 12 |
|
| 13 |
+
# Lấy API Key và URL từ biến môi trường
|
| 14 |
api_key = os.getenv("HF_API_KEY")
|
| 15 |
+
image_api_url = os.getenv("IMAGE_API_URL")
|
| 16 |
headers = {"Authorization": f"Bearer {api_key}"}
|
| 17 |
|
| 18 |
# Hàm gọi API Hugging Face để tạo hình ảnh
|
|
|
|
| 20 |
response = requests.post(image_api_url, headers=headers, json=payload)
|
| 21 |
return response.content
|
| 22 |
|
| 23 |
+
# Hàm dịch tiếng Việt sang tiếng Anh bằng mtranslate
|
| 24 |
def translate_to_english(text):
|
| 25 |
return translate(text, "en", "vi") # Dịch từ tiếng Việt sang tiếng Anh
|
| 26 |
|
|
|
|
| 28 |
def generate_image(prompt):
|
| 29 |
# Dịch prompt từ tiếng Việt sang tiếng Anh
|
| 30 |
prompt_en = translate_to_english(prompt)
|
| 31 |
+
# Thêm yếu tố ngẫu nhiên vào prompt để tạo hình ảnh khác nhau
|
| 32 |
+
random_factor = random.randint(1, 1000)
|
| 33 |
+
prompt_with_randomness = f"{prompt_en}. Seed: {random_factor}"
|
| 34 |
+
|
| 35 |
# Gửi prompt đã dịch vào API tạo hình ảnh
|
| 36 |
+
image_bytes = query({"inputs": prompt_with_randomness})
|
| 37 |
# Mở hình ảnh từ byte dữ liệu
|
| 38 |
image = Image.open(io.BytesIO(image_bytes))
|
| 39 |
return image
|
| 40 |
|
| 41 |
+
# Tạo giao diện Gradio
|
| 42 |
iface = gr.Interface(
|
| 43 |
fn=generate_image,
|
| 44 |
inputs="text",
|
| 45 |
outputs="image",
|
| 46 |
+
title="Image Generator - Trần Như Tuấn",
|
| 47 |
+
description="Nhập mô tả của bạn để tạo hình ảnh"
|
| 48 |
)
|
| 49 |
|
| 50 |
# Khởi chạy ứng dụng
|