Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -15,9 +15,26 @@ 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
|
19 |
def query(payload):
|
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
|
@@ -29,8 +46,13 @@ def generate_image(prompt, view, style, hair_color, eye_color, lip_color):
|
|
29 |
# Dịch prompt từ tiếng Việt sang tiếng Anh
|
30 |
prompt_en = translate_to_english(prompt)
|
31 |
|
|
|
|
|
|
|
|
|
|
|
32 |
# Thêm các yếu tố lựa chọn vào prompt
|
33 |
-
prompt_with_choices = f"{prompt_en}, View: {view}, Style: {style}, Hair color: {
|
34 |
|
35 |
# Thêm yếu tố ngẫu nhiên vào prompt
|
36 |
random_factor = random.randint(1, 1000)
|
@@ -50,9 +72,18 @@ iface = gr.Interface(
|
|
50 |
"text", # Prompt
|
51 |
gr.Dropdown(["Cận cảnh", "Nửa người", "Toàn thân"], label="View"),
|
52 |
gr.Dropdown(["Art", "Anime", "Realistic"], label="Style"),
|
53 |
-
gr.
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
],
|
57 |
outputs="image",
|
58 |
title="Image Generator - Trần Như Tuấn",
|
|
|
15 |
image_api_url = os.getenv("IMAGE_API_URL")
|
16 |
headers = {"Authorization": f"Bearer {api_key}"}
|
17 |
|
18 |
+
# Danh sách màu sắc và mã màu hex tương ứng
|
19 |
+
color_map = {
|
20 |
+
"Đỏ": "#FF0000",
|
21 |
+
"Cam": "#FF7F00",
|
22 |
+
"Vàng": "#FFFF00",
|
23 |
+
"Lục": "#00FF00",
|
24 |
+
"Lam": "#0000FF",
|
25 |
+
"Chàm": "#4B0082",
|
26 |
+
"Tím": "#800080",
|
27 |
+
"Trắng": "#FFFFFF",
|
28 |
+
"Đen": "#000000",
|
29 |
+
"Xám": "#808080",
|
30 |
+
"Cầu vồng": "#FF69B4", # Ví dụ màu cầu vồng, có thể thay bằng màu sắc khác nếu cần
|
31 |
+
}
|
32 |
+
|
33 |
# Hàm gọi API Hugging Face để tạo hình ảnh
|
34 |
def query(payload):
|
35 |
response = requests.post(image_api_url, headers=headers, json=payload)
|
36 |
+
if response.status_code != 200:
|
37 |
+
return f"Error: {response.status_code}, {response.text}"
|
38 |
return response.content
|
39 |
|
40 |
# Hàm dịch tiếng Việt sang tiếng Anh
|
|
|
46 |
# Dịch prompt từ tiếng Việt sang tiếng Anh
|
47 |
prompt_en = translate_to_english(prompt)
|
48 |
|
49 |
+
# Lấy mã màu hex tương ứng từ tên màu đã chọn
|
50 |
+
hair_color_hex = color_map.get(hair_color, "#FF0000")
|
51 |
+
eye_color_hex = color_map.get(eye_color, "#FF0000")
|
52 |
+
lip_color_hex = color_map.get(lip_color, "#FF0000")
|
53 |
+
|
54 |
# Thêm các yếu tố lựa chọn vào prompt
|
55 |
+
prompt_with_choices = f"{prompt_en}, View: {view}, Style: {style}, Hair color: {hair_color_hex}, Eye color: {eye_color_hex}, Lip color: {lip_color_hex}"
|
56 |
|
57 |
# Thêm yếu tố ngẫu nhiên vào prompt
|
58 |
random_factor = random.randint(1, 1000)
|
|
|
72 |
"text", # Prompt
|
73 |
gr.Dropdown(["Cận cảnh", "Nửa người", "Toàn thân"], label="View"),
|
74 |
gr.Dropdown(["Art", "Anime", "Realistic"], label="Style"),
|
75 |
+
gr.Dropdown(
|
76 |
+
["Đỏ", "Cam", "Vàng", "Lục", "Lam", "Chàm", "Tím", "Trắng", "Đen", "Xám", "Cầu vồng"],
|
77 |
+
label="Màu tóc"
|
78 |
+
),
|
79 |
+
gr.Dropdown(
|
80 |
+
["Đỏ", "Cam", "Vàng", "Lục", "Lam", "Chàm", "Tím", "Trắng", "Đen", "Xám", "Cầu vồng"],
|
81 |
+
label="Màu mắt"
|
82 |
+
),
|
83 |
+
gr.Dropdown(
|
84 |
+
["Đỏ", "Cam", "Vàng", "Lục", "Lam", "Chàm", "Tím", "Trắng", "Đen", "Xám", "Cầu vồng"],
|
85 |
+
label="Màu môi"
|
86 |
+
),
|
87 |
],
|
88 |
outputs="image",
|
89 |
title="Image Generator - Trần Như Tuấn",
|