nguyenlam0306
commited on
Commit
·
d0f68a4
1
Parent(s):
55b9944
Sinh tuan tu
Browse files
app.py
CHANGED
@@ -8,7 +8,6 @@ import traceback
|
|
8 |
import os
|
9 |
from pathlib import Path
|
10 |
|
11 |
-
|
12 |
# === Thiết lập môi trường ===
|
13 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
14 |
print(f"Device: {device}")
|
@@ -119,28 +118,37 @@ def generate_image(prompt, style):
|
|
119 |
img_byte_arr.seek(0)
|
120 |
return blank, img_byte_arr
|
121 |
|
122 |
-
# === Main processing function ===
|
123 |
-
def
|
124 |
-
|
|
|
|
|
|
|
125 |
title, summary = summarize(article_text)
|
126 |
print(f"Summary title: {title}")
|
|
|
|
|
|
|
|
|
127 |
prompt = generate_prompt(title)
|
128 |
print(f"Generated prompt: {prompt}")
|
|
|
|
|
|
|
|
|
129 |
image, img_bytes = generate_image(prompt, style_choice)
|
130 |
print(f"Image generated: {image.size if image else 'None'}")
|
131 |
-
|
132 |
-
#
|
133 |
temp_dir = "./temp"
|
134 |
os.makedirs(temp_dir, exist_ok=True)
|
135 |
temp_file = os.path.join(temp_dir, f"generated_image_{id(image)}.png")
|
136 |
image.save(temp_file, format="PNG")
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
file_path = temp_file
|
141 |
-
|
142 |
print(f"✅ Process completed")
|
143 |
-
|
144 |
|
145 |
# === Gradio UI ===
|
146 |
def create_app():
|
@@ -148,6 +156,9 @@ def create_app():
|
|
148 |
gr.Markdown("## 📰 Article → 🖼️ Image Generator")
|
149 |
gr.Markdown("Nhập bài viết → Sinh tiêu đề → Tối ưu prompt → Sinh ảnh minh họa tự động")
|
150 |
|
|
|
|
|
|
|
151 |
with gr.Row():
|
152 |
article_input = gr.Textbox(label="📄 Bài viết", lines=10, placeholder="Dán nội dung bài viết ở đây...")
|
153 |
style_dropdown = gr.Dropdown(choices=["Art", "Anime", "Watercolor", "Cyberpunk"], label="🎨 Phong cách ảnh", value="Art")
|
@@ -164,10 +175,11 @@ def create_app():
|
|
164 |
|
165 |
feedback = gr.Radio(["👍 Hài lòng", "👎 Không hài lòng"], label="📊 Bạn có hài lòng với kết quả không?", value=None)
|
166 |
|
|
|
167 |
submit_button.click(
|
168 |
-
fn=
|
169 |
-
inputs=[article_input, style_dropdown],
|
170 |
-
outputs=[title_output, prompt_output, image_output, download_button]
|
171 |
)
|
172 |
|
173 |
return demo
|
|
|
8 |
import os
|
9 |
from pathlib import Path
|
10 |
|
|
|
11 |
# === Thiết lập môi trường ===
|
12 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
13 |
print(f"Device: {device}")
|
|
|
118 |
img_byte_arr.seek(0)
|
119 |
return blank, img_byte_arr
|
120 |
|
121 |
+
# === Main processing function with staged outputs ===
|
122 |
+
def process_step_by_step(article_text, style_choice, state=None):
|
123 |
+
if state is None:
|
124 |
+
state = {"title": None, "prompt": None, "image": None, "file_path": None}
|
125 |
+
|
126 |
+
# Bước 1: Tóm tắt và tạo tiêu đề
|
127 |
title, summary = summarize(article_text)
|
128 |
print(f"Summary title: {title}")
|
129 |
+
state["title"] = title
|
130 |
+
yield state, title, None, None, None
|
131 |
+
|
132 |
+
# Bước 2: Tạo prompt
|
133 |
prompt = generate_prompt(title)
|
134 |
print(f"Generated prompt: {prompt}")
|
135 |
+
state["prompt"] = prompt
|
136 |
+
yield state, title, prompt, None, None
|
137 |
+
|
138 |
+
# Bước 3: Tạo ảnh
|
139 |
image, img_bytes = generate_image(prompt, style_choice)
|
140 |
print(f"Image generated: {image.size if image else 'None'}")
|
141 |
+
|
142 |
+
# Lưu ảnh tạm thời
|
143 |
temp_dir = "./temp"
|
144 |
os.makedirs(temp_dir, exist_ok=True)
|
145 |
temp_file = os.path.join(temp_dir, f"generated_image_{id(image)}.png")
|
146 |
image.save(temp_file, format="PNG")
|
147 |
+
state["image"] = image
|
148 |
+
state["file_path"] = temp_file
|
149 |
+
|
|
|
|
|
150 |
print(f"✅ Process completed")
|
151 |
+
yield state, title, prompt, image, temp_file
|
152 |
|
153 |
# === Gradio UI ===
|
154 |
def create_app():
|
|
|
156 |
gr.Markdown("## 📰 Article → 🖼️ Image Generator")
|
157 |
gr.Markdown("Nhập bài viết → Sinh tiêu đề → Tối ưu prompt → Sinh ảnh minh họa tự động")
|
158 |
|
159 |
+
# State để lưu trữ trạng thái tạm thời
|
160 |
+
state = gr.State(value=None)
|
161 |
+
|
162 |
with gr.Row():
|
163 |
article_input = gr.Textbox(label="📄 Bài viết", lines=10, placeholder="Dán nội dung bài viết ở đây...")
|
164 |
style_dropdown = gr.Dropdown(choices=["Art", "Anime", "Watercolor", "Cyberpunk"], label="🎨 Phong cách ảnh", value="Art")
|
|
|
175 |
|
176 |
feedback = gr.Radio(["👍 Hài lòng", "👎 Không hài lòng"], label="📊 Bạn có hài lòng với kết quả không?", value=None)
|
177 |
|
178 |
+
# Gắn sự kiện nút submit với hàm process từng bước
|
179 |
submit_button.click(
|
180 |
+
fn=process_step_by_step,
|
181 |
+
inputs=[article_input, style_dropdown, state],
|
182 |
+
outputs=[state, title_output, prompt_output, image_output, download_button]
|
183 |
)
|
184 |
|
185 |
return demo
|