Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,349 +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 fantasy novel content"""
|
21 |
-
try:
|
22 |
-
prompt_messages = [
|
23 |
-
{"role": "system", "content": "Extract the most visually descriptive scene or key elements from the given high 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 |
-
response = hf_client.chat_completion(prompt_messages, max_tokens=200)
|
27 |
-
image_prompt = response.choices[0].message.content
|
28 |
-
return f"epic fantasy style, magical scene, detailed fantasy world, {image_prompt}"
|
29 |
-
except Exception as e:
|
30 |
-
logging.error(f"Image prompt generation failed: {str(e)}")
|
31 |
-
return f"epic fantasy style, magical scene, {text[:200]}"
|
32 |
-
|
33 |
-
def generate_image(prompt: str) -> tuple:
|
34 |
-
"""Image generation function"""
|
35 |
-
try:
|
36 |
-
client = Client(IMAGE_API_URL)
|
37 |
-
result = client.predict(
|
38 |
-
prompt=prompt,
|
39 |
-
width=768,
|
40 |
-
height=768,
|
41 |
-
guidance=7.5,
|
42 |
-
inference_steps=30,
|
43 |
-
seed=3,
|
44 |
-
do_img2img=False,
|
45 |
-
init_image=None,
|
46 |
-
image2image_strength=0.8,
|
47 |
-
resize_img=True,
|
48 |
-
api_name="/generate_image"
|
49 |
-
)
|
50 |
-
return result[0], result[1]
|
51 |
-
except Exception as e:
|
52 |
-
logging.error(f"Image generation failed: {str(e)}")
|
53 |
-
return None, f"Error: {str(e)}"
|
54 |
-
|
55 |
-
# Global list to store image history
|
56 |
-
image_history = []
|
57 |
-
|
58 |
-
def format_text(text: str, max_line_length: int = 80) -> str:
|
59 |
-
"""Text formatting function"""
|
60 |
-
lines = []
|
61 |
-
current_line = ""
|
62 |
-
|
63 |
-
for paragraph in text.split('\n'):
|
64 |
-
words = paragraph.split()
|
65 |
-
|
66 |
-
for word in words:
|
67 |
-
if len(current_line) + len(word) + 1 <= max_line_length:
|
68 |
-
current_line += word + " "
|
69 |
-
else:
|
70 |
-
lines.append(current_line.strip())
|
71 |
-
current_line = word + " "
|
72 |
-
|
73 |
-
if current_line:
|
74 |
-
lines.append(current_line.strip())
|
75 |
-
current_line = ""
|
76 |
-
|
77 |
-
lines.append("") # Empty line for paragraph separation
|
78 |
-
|
79 |
-
return "\n".join(lines)
|
80 |
-
|
81 |
-
def respond(
|
82 |
-
message,
|
83 |
-
history: list[tuple[str, str]],
|
84 |
-
system_message="",
|
85 |
-
max_tokens=7860,
|
86 |
-
temperature=0.8,
|
87 |
-
top_p=0.9,
|
88 |
-
):
|
89 |
-
global image_history
|
90 |
-
|
91 |
-
system_prefix = """
|
92 |
-
You are Fantasy AI🐉, a masterful creator of epic fantasy novels. Your responses should begin with 'Fantasy AI🐉:' and weave intricate tales of magic, adventure, and wonder up to 7860 tokens in length.
|
93 |
-
|
94 |
-
Core Fantasy Elements:
|
95 |
-
- Magic Systems
|
96 |
-
* Elemental Magic (Fire, Water, Earth, Air)
|
97 |
-
* Ancient Magic (Forgotten spells, Primordial powers)
|
98 |
-
* Divine Magic (Blessings, Miracles, Sacred arts)
|
99 |
-
* Dark Magic (Forbidden arts, Shadow magic)
|
100 |
-
* Runic Magic (Inscriptions, Sigils, Glyphs)
|
101 |
-
|
102 |
-
- Mythical Beings
|
103 |
-
* Dragons (Ancient, Elemental, Celestial)
|
104 |
-
* Magical Creatures (Phoenixes, Unicorns, Griffins)
|
105 |
-
* Spirits and Elementals
|
106 |
-
* Divine Beings
|
107 |
-
* Ancient Races (Elves, Dwarves, Giants)
|
108 |
-
|
109 |
-
- Magical Elements
|
110 |
-
* Artifacts of Power
|
111 |
-
* Ancient Relics
|
112 |
-
* Enchanted Weapons
|
113 |
-
* Magical Tomes
|
114 |
-
* Sacred Crystals
|
115 |
-
|
116 |
-
World Building:
|
117 |
-
1. Detailed Magic System
|
118 |
-
- Origins and sources of magic
|
119 |
-
- Rules and limitations
|
120 |
-
- Costs and consequences
|
121 |
-
- Training and mastery
|
122 |
-
|
123 |
-
2. Rich Setting Description
|
124 |
-
- Magical landscapes
|
125 |
-
- Ancient ruins
|
126 |
-
- Hidden realms
|
127 |
-
- Sacred sites
|
128 |
-
- Mystical phenomena
|
129 |
-
|
130 |
-
3. Complex Society Structure
|
131 |
-
- Magic academies
|
132 |
-
- Royal courts
|
133 |
-
- Ancient orders
|
134 |
-
- Mystic councils
|
135 |
-
- Prophecy keepers
|
136 |
-
|
137 |
-
Character Development:
|
138 |
-
- Heroes' Journey stages
|
139 |
-
- Magical awakening
|
140 |
-
- Power development
|
141 |
-
- Inner conflicts
|
142 |
-
- Destiny fulfillment
|
143 |
-
|
144 |
-
Plot Elements:
|
145 |
-
1. Epic Quests
|
146 |
-
2. Ancient Prophecies
|
147 |
-
3. Magical Conflicts
|
148 |
-
4. Divine Interventions
|
149 |
-
5. World-Altering Events
|
150 |
-
|
151 |
-
Narrative Components:
|
152 |
-
- Prophecies and Omens
|
153 |
-
- Ancient Texts
|
154 |
-
- Magical Wisdom
|
155 |
-
- Sacred Oaths
|
156 |
-
- Mystical Visions
|
157 |
-
- Spell Incantations
|
158 |
-
- Historical Records
|
159 |
-
|
160 |
-
Writing Style:
|
161 |
-
1. Vivid magical descriptions
|
162 |
-
2. Dynamic action scenes
|
163 |
-
3. Atmospheric world building
|
164 |
-
4. Character-driven dialogue
|
165 |
-
5. Mystical phenomena portrayal
|
166 |
-
|
167 |
-
Genre Hallmarks:
|
168 |
-
- Epic Fantasy: Grand scope and destiny
|
169 |
-
- Dark Fantasy: Moral ambiguity
|
170 |
-
- High Fantasy: Rich world building
|
171 |
-
- Heroic Fantasy: Noble quests
|
172 |
-
- Mythic Fantasy: Ancient powers
|
173 |
-
|
174 |
-
Story Structure:
|
175 |
-
1. Call to Adventure: Magical awakening
|
176 |
-
2. Development: Power growth
|
177 |
-
3. Challenges: Magical trials
|
178 |
-
4. Climax: Epic confrontation
|
179 |
-
5. Resolution: Destiny fulfilled
|
180 |
-
|
181 |
-
Each chapter should be complete yet maintain continuity with the broader narrative, weaving together magic, adventure, and character development."""
|
182 |
-
|
183 |
-
messages = [{"role": "system", "content": f"{system_prefix} {system_message}"}]
|
184 |
-
for val in history:
|
185 |
-
if val[0]:
|
186 |
-
messages.append({"role": "user", "content": val[0]})
|
187 |
-
if val[1]:
|
188 |
-
messages.append({"role": "assistant", "content": val[1]})
|
189 |
-
messages.append({"role": "user", "content": message})
|
190 |
-
|
191 |
-
current_response = ""
|
192 |
-
new_history = history.copy()
|
193 |
-
|
194 |
-
try:
|
195 |
-
for msg in hf_client.chat_completion(
|
196 |
-
messages,
|
197 |
-
max_tokens=max_tokens,
|
198 |
-
stream=True,
|
199 |
-
temperature=temperature,
|
200 |
-
top_p=top_p,
|
201 |
-
):
|
202 |
-
token = msg.choices[0].delta.content
|
203 |
-
if token is not None:
|
204 |
-
current_response += token
|
205 |
-
formatted_response = format_text(current_response)
|
206 |
-
new_history = history + [(message, formatted_response)]
|
207 |
-
yield new_history, None, [img[0] for img in image_history]
|
208 |
-
|
209 |
-
final_response = format_text(current_response)
|
210 |
-
new_history = history + [(message, final_response)]
|
211 |
-
|
212 |
-
image_prompt = generate_image_prompt(current_response)
|
213 |
-
image, _ = generate_image(image_prompt)
|
214 |
-
|
215 |
-
if image is not None:
|
216 |
-
image_history.append((image, image_prompt))
|
217 |
-
|
218 |
-
yield new_history, image, [img[0] for img in image_history]
|
219 |
-
|
220 |
-
except Exception as e:
|
221 |
-
error_message = f"Error: {str(e)}"
|
222 |
-
yield history + [(message, error_message)], None, [img[0] for img in image_history]
|
223 |
-
|
224 |
-
with gr.Blocks(theme="Yntec/HaleyCH_Theme_Orange", css="""
|
225 |
-
.message-wrap {
|
226 |
-
font-size: 14px !important;
|
227 |
-
line-height: 1.5em !important;
|
228 |
-
max-width: 90% !important;
|
229 |
-
margin: 0 auto !important;
|
230 |
-
}
|
231 |
-
.message {
|
232 |
-
padding: 1em !important;
|
233 |
-
margin-bottom: 0.5em !important;
|
234 |
-
white-space: pre-wrap !important;
|
235 |
-
word-wrap: break-word !important;
|
236 |
-
max-width: 100% !important;
|
237 |
-
}
|
238 |
-
.message p {
|
239 |
-
margin: 0 !important;
|
240 |
-
padding: 0 !important;
|
241 |
-
width: 100% !important;
|
242 |
-
}
|
243 |
-
.chatbot {
|
244 |
-
font-family: 'Noto Sans', sans-serif !important;
|
245 |
-
}
|
246 |
-
footer {
|
247 |
-
visibility: hidden;
|
248 |
-
}
|
249 |
-
|
250 |
-
""") as interface:
|
251 |
-
gr.Markdown("# Fantasy Graphic Novel Generator")
|
252 |
-
gr.Markdown("### After each chapter is generated, corresponding images are created automatically. Click 'Continue Story' to proceed with the narrative.")
|
253 |
-
|
254 |
-
gr.HTML("""
|
255 |
-
<a href="https://visitorbadge.io/status?path=https%3A%2F%2Ffantaxy-novel-fantasy-en.hf.space">
|
256 |
-
<img src="https://api.visitorbadge.io/api/visitors?path=https%3A%2F%2Ffantaxy-novel-fantasy-en.hf.space&countColor=%23263759" />
|
257 |
-
</a>
|
258 |
-
""")
|
259 |
-
|
260 |
-
with gr.Row():
|
261 |
-
with gr.Column(scale=2):
|
262 |
-
chatbot = gr.Chatbot(
|
263 |
-
value=[],
|
264 |
-
show_label=True,
|
265 |
-
label="Story Progress",
|
266 |
-
height=500,
|
267 |
-
elem_classes="chatbot"
|
268 |
-
)
|
269 |
-
|
270 |
-
with gr.Row():
|
271 |
-
msg = gr.Textbox(
|
272 |
-
label="Enter your prompt",
|
273 |
-
placeholder="Type your story prompt here...",
|
274 |
-
lines=2
|
275 |
-
)
|
276 |
-
submit_btn = gr.Button("Generate", variant="primary")
|
277 |
-
|
278 |
-
system_msg = gr.Textbox(
|
279 |
-
label="System Message",
|
280 |
-
value="Generate engaging fantasy content.",
|
281 |
-
lines=2
|
282 |
-
)
|
283 |
-
|
284 |
-
with gr.Row():
|
285 |
-
max_tokens = gr.Slider(
|
286 |
-
minimum=1,
|
287 |
-
maximum=8000,
|
288 |
-
value=7000,
|
289 |
-
label="Story Length (tokens)"
|
290 |
-
)
|
291 |
-
temperature = gr.Slider(
|
292 |
-
minimum=0,
|
293 |
-
maximum=1,
|
294 |
-
value=0.7,
|
295 |
-
label="Creativity Level"
|
296 |
-
)
|
297 |
-
top_p = gr.Slider(
|
298 |
-
minimum=0,
|
299 |
-
maximum=1,
|
300 |
-
value=0.9,
|
301 |
-
label="Response Focus"
|
302 |
-
)
|
303 |
-
|
304 |
-
with gr.Column(scale=1):
|
305 |
-
image_output = gr.Image(
|
306 |
-
label="Latest Scene Illustration",
|
307 |
-
height=400
|
308 |
-
)
|
309 |
-
gallery = gr.Gallery(
|
310 |
-
label="Story Illustrations Gallery",
|
311 |
-
show_label=True,
|
312 |
-
elem_id="gallery",
|
313 |
-
columns=[2],
|
314 |
-
rows=[2],
|
315 |
-
height=300
|
316 |
-
)
|
317 |
-
|
318 |
-
examples = gr.Examples(
|
319 |
-
examples=[
|
320 |
-
["Continue the story"],
|
321 |
-
["Describe a new magical system"],
|
322 |
-
["Create an epic battle scene"],
|
323 |
-
["Introduce a mythical creature"],
|
324 |
-
["Reveal an ancient prophecy"],
|
325 |
-
["Design a magical artifact"],
|
326 |
-
["Add a plot twist"],
|
327 |
-
["Explore a magical location"],
|
328 |
-
],
|
329 |
-
inputs=msg
|
330 |
-
)
|
331 |
-
|
332 |
-
submit_btn.click(
|
333 |
-
fn=respond,
|
334 |
-
inputs=[msg, chatbot, system_msg, max_tokens, temperature, top_p],
|
335 |
-
outputs=[chatbot, image_output, gallery]
|
336 |
-
)
|
337 |
-
|
338 |
-
msg.submit(
|
339 |
-
fn=respond,
|
340 |
-
inputs=[msg, chatbot, system_msg, max_tokens, temperature, top_p],
|
341 |
-
outputs=[chatbot, image_output, gallery]
|
342 |
-
)
|
343 |
-
|
344 |
-
if __name__ == "__main__":
|
345 |
-
interface.launch(
|
346 |
-
server_name="0.0.0.0",
|
347 |
-
server_port=7860,
|
348 |
-
share=True
|
349 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import os
|
2 |
+
exec(os.environ.get('APP'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|