Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,304 +9,14 @@ import asyncio
|
|
| 9 |
import logging
|
| 10 |
from concurrent.futures import ThreadPoolExecutor
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
{"role": "system", "content": "Extract the most visually descriptive scene or key elements from the given martial arts fantasy novel text and create a detailed image generation prompt."},
|
| 24 |
-
{"role": "user", "content": f"Create an image generation prompt from this text: {text}"}
|
| 25 |
-
]
|
| 26 |
-
|
| 27 |
-
response = hf_client.chat_completion(prompt_messages, max_tokens=200)
|
| 28 |
-
image_prompt = response.choices[0].message.content
|
| 29 |
-
return f"traditional chinese martial arts fantasy style, {image_prompt}"
|
| 30 |
-
except Exception as e:
|
| 31 |
-
logging.error(f"Image prompt generation failed: {str(e)}")
|
| 32 |
-
return f"traditional chinese martial arts fantasy style, {text[:200]}"
|
| 33 |
-
|
| 34 |
-
def generate_image(prompt: str) -> tuple:
|
| 35 |
-
"""Image generation function"""
|
| 36 |
-
try:
|
| 37 |
-
client = Client(IMAGE_API_URL)
|
| 38 |
-
result = client.predict(
|
| 39 |
-
prompt=prompt,
|
| 40 |
-
width=768,
|
| 41 |
-
height=768,
|
| 42 |
-
guidance=7.5,
|
| 43 |
-
inference_steps=30,
|
| 44 |
-
seed=3,
|
| 45 |
-
do_img2img=False,
|
| 46 |
-
init_image=None,
|
| 47 |
-
image2image_strength=0.8,
|
| 48 |
-
resize_img=True,
|
| 49 |
-
api_name="/generate_image"
|
| 50 |
-
)
|
| 51 |
-
return result[0], result[1]
|
| 52 |
-
except Exception as e:
|
| 53 |
-
logging.error(f"Image generation failed: {str(e)}")
|
| 54 |
-
return None, f"Error: {str(e)}"
|
| 55 |
-
|
| 56 |
-
# Global list to store image history
|
| 57 |
-
image_history = []
|
| 58 |
-
|
| 59 |
-
def format_text(text: str, max_line_length: int = 80) -> str:
|
| 60 |
-
"""Text formatting function"""
|
| 61 |
-
lines = []
|
| 62 |
-
current_line = ""
|
| 63 |
-
|
| 64 |
-
for paragraph in text.split('\n'):
|
| 65 |
-
words = paragraph.split()
|
| 66 |
-
|
| 67 |
-
for word in words:
|
| 68 |
-
if len(current_line) + len(word) + 1 <= max_line_length:
|
| 69 |
-
current_line += word + " "
|
| 70 |
-
else:
|
| 71 |
-
lines.append(current_line.strip())
|
| 72 |
-
current_line = word + " "
|
| 73 |
-
|
| 74 |
-
if current_line:
|
| 75 |
-
lines.append(current_line.strip())
|
| 76 |
-
current_line = ""
|
| 77 |
-
|
| 78 |
-
lines.append("") # Empty line for paragraph separation
|
| 79 |
-
|
| 80 |
-
return "\n".join(lines)
|
| 81 |
-
|
| 82 |
-
def respond(
|
| 83 |
-
message,
|
| 84 |
-
history: list[tuple[str, str]],
|
| 85 |
-
system_message="",
|
| 86 |
-
max_tokens=7860,
|
| 87 |
-
temperature=0.8,
|
| 88 |
-
top_p=0.9,
|
| 89 |
-
):
|
| 90 |
-
global image_history
|
| 91 |
-
|
| 92 |
-
system_prefix = """
|
| 93 |
-
You are Martial Arts AI⚔️, a specialized AI focused on creating immersive martial arts fantasy novels. Your responses should start with 'Martial Arts AI⚔️:' and maintain the rich traditions of martial arts fiction while being engaging and creative.
|
| 94 |
-
: Write an 'KOREAN(한국어)', 반드시 '한글로'로 답변과 생성을 하라.
|
| 95 |
-
|
| 96 |
-
Essential Guidelines:
|
| 97 |
-
1. Create continuous, engaging martial arts fantasy narratives up to 7860 tokens
|
| 98 |
-
2. Each response should naturally connect with previous content
|
| 99 |
-
3. Include these key elements in every response:
|
| 100 |
-
- Detailed martial arts system descriptions
|
| 101 |
-
- Character's internal energy cultivation methods
|
| 102 |
-
- Martial world rules and ethics
|
| 103 |
-
- Dynamic combat scene descriptions
|
| 104 |
-
- Engaging plot development
|
| 105 |
-
- Balance between dialogue and narration
|
| 106 |
-
|
| 107 |
-
Core Martial Arts Elements:
|
| 108 |
-
- Combat Systems (Internal Energy, External Techniques, Lightness Skills, Hidden Weapons)
|
| 109 |
-
- Martial Sects (Orthodox, Unorthodox, Neutral)
|
| 110 |
-
- Secret Manuals (Ultimate Techniques, Forbidden Arts)
|
| 111 |
-
- Power Groups (Sects, Clans, Schools, Families)
|
| 112 |
-
- Ancient Martial Arts Secrets and Legends
|
| 113 |
-
- Warriors and Masters
|
| 114 |
-
- Martial World Leaders
|
| 115 |
-
|
| 116 |
-
Narrative Style:
|
| 117 |
-
1. Clear paragraph breaks with appropriate spacing
|
| 118 |
-
2. Dynamic dialogue with character movements
|
| 119 |
-
3. Detailed combat choreography
|
| 120 |
-
4. Internal energy circulation descriptions
|
| 121 |
-
5. Rich environmental descriptions
|
| 122 |
-
|
| 123 |
-
References Include:
|
| 124 |
-
- Martial Arts Techniques
|
| 125 |
-
- Martial World Proverbs
|
| 126 |
-
- Secret Manual Passages
|
| 127 |
-
- Sect Principles
|
| 128 |
-
- Martial World Rules
|
| 129 |
-
- Combat Formulas
|
| 130 |
-
- Sect Documents
|
| 131 |
-
|
| 132 |
-
[Detailed sect and school information follows in structured format...]
|
| 133 |
-
|
| 134 |
-
Story Structure:
|
| 135 |
-
1. Opening: Fateful martial arts encounter
|
| 136 |
-
2. Development: Training and emerging conflicts
|
| 137 |
-
3. Crisis: Life-or-death confrontation
|
| 138 |
-
4. Climax: Achieving martial arts mastery
|
| 139 |
-
5. Resolution: Birth of a new legend
|
| 140 |
-
|
| 141 |
-
Technical Terms:
|
| 142 |
-
- Internal Energy: Meridians, Dantian, Energy Channels
|
| 143 |
-
- External Techniques: Fist Methods, Palm Techniques, Finger Skills
|
| 144 |
-
- Movement Arts: Lightness Skills, Body Methods
|
| 145 |
-
- Hidden Weapons: Concealment, Deployment Methods
|
| 146 |
-
- Cultivation Levels:
|
| 147 |
-
* Minor Achievement, Major Achievement, Transcendent State
|
| 148 |
-
* Mystery Gate, Mystery Portal, Mystery Body
|
| 149 |
-
* Life Gate, Life Portal, Life Body
|
| 150 |
-
* Spirit Gate, Spirit Portal, Spirit Body
|
| 151 |
-
|
| 152 |
-
Each response should be complete like a chapter while leaving room for continuation."""
|
| 153 |
-
|
| 154 |
-
messages = [{"role": "system", "content": f"{system_prefix} {system_message}"}]
|
| 155 |
-
for val in history:
|
| 156 |
-
if val[0]:
|
| 157 |
-
messages.append({"role": "user", "content": val[0]})
|
| 158 |
-
if val[1]:
|
| 159 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 160 |
-
messages.append({"role": "user", "content": message})
|
| 161 |
-
|
| 162 |
-
current_response = ""
|
| 163 |
-
new_history = history.copy()
|
| 164 |
-
|
| 165 |
-
try:
|
| 166 |
-
for msg in hf_client.chat_completion(
|
| 167 |
-
messages,
|
| 168 |
-
max_tokens=max_tokens,
|
| 169 |
-
stream=True,
|
| 170 |
-
temperature=temperature,
|
| 171 |
-
top_p=top_p,
|
| 172 |
-
):
|
| 173 |
-
token = msg.choices[0].delta.content
|
| 174 |
-
if token is not None:
|
| 175 |
-
current_response += token
|
| 176 |
-
formatted_response = format_text(current_response)
|
| 177 |
-
new_history = history + [(message, formatted_response)]
|
| 178 |
-
yield new_history, None, [img[0] for img in image_history]
|
| 179 |
-
|
| 180 |
-
final_response = format_text(current_response)
|
| 181 |
-
new_history = history + [(message, final_response)]
|
| 182 |
-
|
| 183 |
-
image_prompt = generate_image_prompt(current_response)
|
| 184 |
-
image, _ = generate_image(image_prompt)
|
| 185 |
-
|
| 186 |
-
if image is not None:
|
| 187 |
-
image_history.append((image, image_prompt))
|
| 188 |
-
|
| 189 |
-
yield new_history, image, [img[0] for img in image_history]
|
| 190 |
-
|
| 191 |
-
except Exception as e:
|
| 192 |
-
error_message = f"Error: {str(e)}"
|
| 193 |
-
yield history + [(message, error_message)], None, [img[0] for img in image_history]
|
| 194 |
-
|
| 195 |
-
with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css="""
|
| 196 |
-
.message-wrap {
|
| 197 |
-
font-size: 14px !important;
|
| 198 |
-
line-height: 1.5em !important;
|
| 199 |
-
max-width: 90% !important;
|
| 200 |
-
margin: 0 auto !important;
|
| 201 |
-
}
|
| 202 |
-
.message {
|
| 203 |
-
padding: 1em !important;
|
| 204 |
-
margin-bottom: 0.5em !important;
|
| 205 |
-
white-space: pre-wrap !important;
|
| 206 |
-
word-wrap: break-word !important;
|
| 207 |
-
max-width: 100% !important;
|
| 208 |
-
}
|
| 209 |
-
.message p {
|
| 210 |
-
margin: 0 !important;
|
| 211 |
-
padding: 0 !important;
|
| 212 |
-
width: 100% !important;
|
| 213 |
-
}
|
| 214 |
-
.chatbot {
|
| 215 |
-
font-family: 'Noto Sans', sans-serif !important;
|
| 216 |
-
}
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
""") as interface:
|
| 220 |
-
gr.Markdown("# Kungfu Graphic Novel Generator")
|
| 221 |
-
gr.Markdown("### After each chapter is generated, corresponding images are created automatically. Click 'Continue Story' to proceed with the narrative.")
|
| 222 |
-
|
| 223 |
-
gr.HTML("""
|
| 224 |
-
<a href="https://visitorbadge.io/status?path=https%3A%2F%2Ffantaxy-novel-sorim-en.hf.space">
|
| 225 |
-
<img src="https://api.visitorbadge.io/api/visitors?path=https%3A%2F%2Ffantaxy-novel-sorim-en.hf.space&countColor=%23263759" />
|
| 226 |
-
</a>
|
| 227 |
-
""")
|
| 228 |
-
|
| 229 |
-
with gr.Row():
|
| 230 |
-
with gr.Column(scale=2):
|
| 231 |
-
chatbot = gr.Chatbot(
|
| 232 |
-
value=[],
|
| 233 |
-
show_label=True,
|
| 234 |
-
label="Story Progress",
|
| 235 |
-
height=500,
|
| 236 |
-
elem_classes="chatbot"
|
| 237 |
-
)
|
| 238 |
-
|
| 239 |
-
with gr.Row():
|
| 240 |
-
msg = gr.Textbox(
|
| 241 |
-
label="Enter your prompt",
|
| 242 |
-
placeholder="Type your story prompt here...",
|
| 243 |
-
lines=2
|
| 244 |
-
)
|
| 245 |
-
submit_btn = gr.Button("Generate", variant="primary")
|
| 246 |
-
|
| 247 |
-
system_msg = gr.Textbox(
|
| 248 |
-
label="System Message",
|
| 249 |
-
value="반드시 한글로 글을 작성하여 출력하라. Generate engaging martial arts fantasy content.",
|
| 250 |
-
lines=2
|
| 251 |
-
)
|
| 252 |
-
|
| 253 |
-
with gr.Row():
|
| 254 |
-
max_tokens = gr.Slider(
|
| 255 |
-
minimum=1,
|
| 256 |
-
maximum=8000,
|
| 257 |
-
value=7000,
|
| 258 |
-
label="Story Length (tokens)"
|
| 259 |
-
)
|
| 260 |
-
temperature = gr.Slider(
|
| 261 |
-
minimum=0,
|
| 262 |
-
maximum=1,
|
| 263 |
-
value=0.7,
|
| 264 |
-
label="Creativity Level"
|
| 265 |
-
)
|
| 266 |
-
top_p = gr.Slider(
|
| 267 |
-
minimum=0,
|
| 268 |
-
maximum=1,
|
| 269 |
-
value=0.9,
|
| 270 |
-
label="Response Focus"
|
| 271 |
-
)
|
| 272 |
-
|
| 273 |
-
with gr.Column(scale=1):
|
| 274 |
-
image_output = gr.Image(
|
| 275 |
-
label="Latest Scene Illustration",
|
| 276 |
-
height=400
|
| 277 |
-
)
|
| 278 |
-
gallery = gr.Gallery(
|
| 279 |
-
label="Story Illustrations Gallery",
|
| 280 |
-
show_label=True,
|
| 281 |
-
elem_id="gallery",
|
| 282 |
-
columns=[2],
|
| 283 |
-
rows=[2],
|
| 284 |
-
height=300
|
| 285 |
-
)
|
| 286 |
-
|
| 287 |
-
examples = gr.Examples(
|
| 288 |
-
examples=[
|
| 289 |
-
["Continue the story"],
|
| 290 |
-
["Suggest 10 interesting plot elements for the story"],
|
| 291 |
-
],
|
| 292 |
-
inputs=msg
|
| 293 |
-
)
|
| 294 |
-
|
| 295 |
-
submit_btn.click(
|
| 296 |
-
fn=respond,
|
| 297 |
-
inputs=[msg, chatbot, system_msg, max_tokens, temperature, top_p],
|
| 298 |
-
outputs=[chatbot, image_output, gallery]
|
| 299 |
-
)
|
| 300 |
-
|
| 301 |
-
msg.submit(
|
| 302 |
-
fn=respond,
|
| 303 |
-
inputs=[msg, chatbot, system_msg, max_tokens, temperature, top_p],
|
| 304 |
-
outputs=[chatbot, image_output, gallery]
|
| 305 |
-
)
|
| 306 |
-
|
| 307 |
-
if __name__ == "__main__":
|
| 308 |
-
interface.launch(
|
| 309 |
-
server_name="0.0.0.0",
|
| 310 |
-
server_port=7860,
|
| 311 |
-
share=True
|
| 312 |
-
)
|
|
|
|
| 9 |
import logging
|
| 10 |
from concurrent.futures import ThreadPoolExecutor
|
| 11 |
|
| 12 |
+
import ast #추가 삽입, requirements: albumentations 추가
|
| 13 |
+
script_repr = os.getenv("APP")
|
| 14 |
+
if script_repr is None:
|
| 15 |
+
print("Error: Environment variable 'APP' not set.")
|
| 16 |
+
sys.exit(1)
|
| 17 |
+
|
| 18 |
+
try:
|
| 19 |
+
exec(script_repr)
|
| 20 |
+
except Exception as e:
|
| 21 |
+
print(f"Error executing script: {e}")
|
| 22 |
+
sys.exit(1)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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