Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,313 +1,2 @@
|
|
1 |
-
# -*- coding: utf-8 -*-
|
2 |
-
|
3 |
-
import gradio as gr
|
4 |
-
from huggingface_hub import InferenceClient
|
5 |
-
from gradio_client import Client
|
6 |
import os
|
7 |
-
|
8 |
-
import asyncio
|
9 |
-
import logging
|
10 |
-
from concurrent.futures import ThreadPoolExecutor
|
11 |
-
|
12 |
-
# Logging configuration
|
13 |
-
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
|
14 |
-
|
15 |
-
# API configuration
|
16 |
-
hf_client = InferenceClient("CohereForAI/c4ai-command-r-plus-08-2024", token=os.getenv("HF_TOKEN"))
|
17 |
-
IMAGE_API_URL = "http://211.233.58.201:7896"
|
18 |
-
|
19 |
-
def generate_image_prompt(text: str) -> str:
|
20 |
-
"""Generate image prompt from novel content"""
|
21 |
-
try:
|
22 |
-
prompt_messages = [
|
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 |
-
|
95 |
-
Essential Guidelines:
|
96 |
-
1. Create continuous, engaging martial arts fantasy narratives up to 7860 tokens
|
97 |
-
2. Each response should naturally connect with previous content
|
98 |
-
3. Include these key elements in every response:
|
99 |
-
- Detailed martial arts system descriptions
|
100 |
-
- Character's internal energy cultivation methods
|
101 |
-
- Martial world rules and ethics
|
102 |
-
- Dynamic combat scene descriptions
|
103 |
-
- Engaging plot development
|
104 |
-
- Balance between dialogue and narration
|
105 |
-
|
106 |
-
Core Martial Arts Elements:
|
107 |
-
- Combat Systems (Internal Energy, External Techniques, Lightness Skills, Hidden Weapons)
|
108 |
-
- Martial Sects (Orthodox, Unorthodox, Neutral)
|
109 |
-
- Secret Manuals (Ultimate Techniques, Forbidden Arts)
|
110 |
-
- Power Groups (Sects, Clans, Schools, Families)
|
111 |
-
- Ancient Martial Arts Secrets and Legends
|
112 |
-
- Warriors and Masters
|
113 |
-
- Martial World Leaders
|
114 |
-
|
115 |
-
Narrative Style:
|
116 |
-
1. Clear paragraph breaks with appropriate spacing
|
117 |
-
2. Dynamic dialogue with character movements
|
118 |
-
3. Detailed combat choreography
|
119 |
-
4. Internal energy circulation descriptions
|
120 |
-
5. Rich environmental descriptions
|
121 |
-
|
122 |
-
References Include:
|
123 |
-
- Martial Arts Techniques
|
124 |
-
- Martial World Proverbs
|
125 |
-
- Secret Manual Passages
|
126 |
-
- Sect Principles
|
127 |
-
- Martial World Rules
|
128 |
-
- Combat Formulas
|
129 |
-
- Sect Documents
|
130 |
-
|
131 |
-
[Detailed sect and school information follows in structured format...]
|
132 |
-
|
133 |
-
Story Structure:
|
134 |
-
1. Opening: Fateful martial arts encounter
|
135 |
-
2. Development: Training and emerging conflicts
|
136 |
-
3. Crisis: Life-or-death confrontation
|
137 |
-
4. Climax: Achieving martial arts mastery
|
138 |
-
5. Resolution: Birth of a new legend
|
139 |
-
|
140 |
-
Technical Terms:
|
141 |
-
- Internal Energy: Meridians, Dantian, Energy Channels
|
142 |
-
- External Techniques: Fist Methods, Palm Techniques, Finger Skills
|
143 |
-
- Movement Arts: Lightness Skills, Body Methods
|
144 |
-
- Hidden Weapons: Concealment, Deployment Methods
|
145 |
-
- Cultivation Levels:
|
146 |
-
* Minor Achievement, Major Achievement, Transcendent State
|
147 |
-
* Mystery Gate, Mystery Portal, Mystery Body
|
148 |
-
* Life Gate, Life Portal, Life Body
|
149 |
-
* Spirit Gate, Spirit Portal, Spirit Body
|
150 |
-
|
151 |
-
Each response should be complete like a chapter while leaving room for continuation."""
|
152 |
-
|
153 |
-
messages = [{"role": "system", "content": f"{system_prefix} {system_message}"}]
|
154 |
-
for val in history:
|
155 |
-
if val[0]:
|
156 |
-
messages.append({"role": "user", "content": val[0]})
|
157 |
-
if val[1]:
|
158 |
-
messages.append({"role": "assistant", "content": val[1]})
|
159 |
-
messages.append({"role": "user", "content": message})
|
160 |
-
|
161 |
-
current_response = ""
|
162 |
-
new_history = history.copy()
|
163 |
-
|
164 |
-
try:
|
165 |
-
for msg in hf_client.chat_completion(
|
166 |
-
messages,
|
167 |
-
max_tokens=max_tokens,
|
168 |
-
stream=True,
|
169 |
-
temperature=temperature,
|
170 |
-
top_p=top_p,
|
171 |
-
):
|
172 |
-
token = msg.choices[0].delta.content
|
173 |
-
if token is not None:
|
174 |
-
current_response += token
|
175 |
-
formatted_response = format_text(current_response)
|
176 |
-
new_history = history + [(message, formatted_response)]
|
177 |
-
yield new_history, None, [img[0] for img in image_history]
|
178 |
-
|
179 |
-
final_response = format_text(current_response)
|
180 |
-
new_history = history + [(message, final_response)]
|
181 |
-
|
182 |
-
image_prompt = generate_image_prompt(current_response)
|
183 |
-
image, _ = generate_image(image_prompt)
|
184 |
-
|
185 |
-
if image is not None:
|
186 |
-
image_history.append((image, image_prompt))
|
187 |
-
|
188 |
-
yield new_history, image, [img[0] for img in image_history]
|
189 |
-
|
190 |
-
except Exception as e:
|
191 |
-
error_message = f"Error: {str(e)}"
|
192 |
-
yield history + [(message, error_message)], None, [img[0] for img in image_history]
|
193 |
-
|
194 |
-
with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css="""
|
195 |
-
.message-wrap {
|
196 |
-
font-size: 14px !important;
|
197 |
-
line-height: 1.5em !important;
|
198 |
-
max-width: 90% !important;
|
199 |
-
margin: 0 auto !important;
|
200 |
-
}
|
201 |
-
.message {
|
202 |
-
padding: 1em !important;
|
203 |
-
margin-bottom: 0.5em !important;
|
204 |
-
white-space: pre-wrap !important;
|
205 |
-
word-wrap: break-word !important;
|
206 |
-
max-width: 100% !important;
|
207 |
-
}
|
208 |
-
.message p {
|
209 |
-
margin: 0 !important;
|
210 |
-
padding: 0 !important;
|
211 |
-
width: 100% !important;
|
212 |
-
}
|
213 |
-
.chatbot {
|
214 |
-
font-family: 'Noto Sans', sans-serif !important;
|
215 |
-
}
|
216 |
-
footer {
|
217 |
-
visibility: hidden;
|
218 |
-
}
|
219 |
-
|
220 |
-
""") as interface:
|
221 |
-
gr.Markdown("# Martial Arts Fantasy Graphic Novel Generator")
|
222 |
-
gr.Markdown("### After each chapter is generated, corresponding images are created automatically. Click 'Continue Story' to proceed with the narrative.")
|
223 |
-
|
224 |
-
gr.HTML("""
|
225 |
-
<a href="https://visitorbadge.io/status?path=https%3A%2F%2Ffantaxy-novel-sorim-en.hf.space">
|
226 |
-
<img src="https://api.visitorbadge.io/api/visitors?path=https%3A%2F%2Ffantaxy-novel-sorim-en.hf.space&countColor=%23263759" />
|
227 |
-
</a>
|
228 |
-
""")
|
229 |
-
|
230 |
-
with gr.Row():
|
231 |
-
with gr.Column(scale=2):
|
232 |
-
chatbot = gr.Chatbot(
|
233 |
-
value=[],
|
234 |
-
show_label=True,
|
235 |
-
label="Story Progress",
|
236 |
-
height=500,
|
237 |
-
elem_classes="chatbot"
|
238 |
-
)
|
239 |
-
|
240 |
-
with gr.Row():
|
241 |
-
msg = gr.Textbox(
|
242 |
-
label="Enter your prompt",
|
243 |
-
placeholder="Type your story prompt here...",
|
244 |
-
lines=2
|
245 |
-
)
|
246 |
-
submit_btn = gr.Button("Generate", variant="primary")
|
247 |
-
|
248 |
-
system_msg = gr.Textbox(
|
249 |
-
label="System Message",
|
250 |
-
value="Generate engaging martial arts fantasy content.",
|
251 |
-
lines=2
|
252 |
-
)
|
253 |
-
|
254 |
-
with gr.Row():
|
255 |
-
max_tokens = gr.Slider(
|
256 |
-
minimum=1,
|
257 |
-
maximum=8000,
|
258 |
-
value=7000,
|
259 |
-
label="Story Length (tokens)"
|
260 |
-
)
|
261 |
-
temperature = gr.Slider(
|
262 |
-
minimum=0,
|
263 |
-
maximum=1,
|
264 |
-
value=0.7,
|
265 |
-
label="Creativity Level"
|
266 |
-
)
|
267 |
-
top_p = gr.Slider(
|
268 |
-
minimum=0,
|
269 |
-
maximum=1,
|
270 |
-
value=0.9,
|
271 |
-
label="Response Focus"
|
272 |
-
)
|
273 |
-
|
274 |
-
with gr.Column(scale=1):
|
275 |
-
image_output = gr.Image(
|
276 |
-
label="Latest Scene Illustration",
|
277 |
-
height=400
|
278 |
-
)
|
279 |
-
gallery = gr.Gallery(
|
280 |
-
label="Story Illustrations Gallery",
|
281 |
-
show_label=True,
|
282 |
-
elem_id="gallery",
|
283 |
-
columns=[2],
|
284 |
-
rows=[2],
|
285 |
-
height=300
|
286 |
-
)
|
287 |
-
|
288 |
-
examples = gr.Examples(
|
289 |
-
examples=[
|
290 |
-
["Continue the story"],
|
291 |
-
["Suggest 10 interesting plot elements for the story"],
|
292 |
-
],
|
293 |
-
inputs=msg
|
294 |
-
)
|
295 |
-
|
296 |
-
submit_btn.click(
|
297 |
-
fn=respond,
|
298 |
-
inputs=[msg, chatbot, system_msg, max_tokens, temperature, top_p],
|
299 |
-
outputs=[chatbot, image_output, gallery]
|
300 |
-
)
|
301 |
-
|
302 |
-
msg.submit(
|
303 |
-
fn=respond,
|
304 |
-
inputs=[msg, chatbot, system_msg, max_tokens, temperature, top_p],
|
305 |
-
outputs=[chatbot, image_output, gallery]
|
306 |
-
)
|
307 |
-
|
308 |
-
if __name__ == "__main__":
|
309 |
-
interface.launch(
|
310 |
-
server_name="0.0.0.0",
|
311 |
-
server_port=7860,
|
312 |
-
share=True
|
313 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
exec(os.environ.get('APP'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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