Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from huggingface_hub import InferenceClient
|
| 4 |
+
|
| 5 |
+
# Load token and private login credentials securely from Space Secrets
|
| 6 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 7 |
+
APP_USER = os.environ.get("APP_USER", "author")
|
| 8 |
+
APP_PASS = os.environ.get("APP_PASS")
|
| 9 |
+
|
| 10 |
+
client = InferenceClient(token=HF_TOKEN)
|
| 11 |
+
|
| 12 |
+
SYSTEM_PROMPT = (
|
| 13 |
+
"You are a master Nobel-caliber book writer and werewolf fiction co-author. "
|
| 14 |
+
"Write atmospheric, emotionally gripping, literary prose with rich world-building, "
|
| 15 |
+
"strictly maintaining pack lore and narrative continuity."
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
def write_chapter(prompt, history, temp):
|
| 19 |
+
if not prompt or not prompt.strip():
|
| 20 |
+
return "Please enter a scene prompt or chapter instruction."
|
| 21 |
+
messages = [{"role": "system", "content": SYSTEM_PROMPT}]
|
| 22 |
+
for user_msg, bot_msg in history:
|
| 23 |
+
messages.append({"role": "user", "content": user_msg})
|
| 24 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 25 |
+
messages.append({"role": "user", "content": prompt})
|
| 26 |
+
|
| 27 |
+
try:
|
| 28 |
+
response = client.chat_completion(
|
| 29 |
+
model="inclusionAI/Ling-3.1-flash",
|
| 30 |
+
messages=messages,
|
| 31 |
+
max_tokens=2048,
|
| 32 |
+
temperature=temp,
|
| 33 |
+
)
|
| 34 |
+
return response.choices.message.content
|
| 35 |
+
except Exception as e:
|
| 36 |
+
return f"β οΈ Notice: {str(e)}"
|
| 37 |
+
|
| 38 |
+
def generate_scene_art(scene_prompt):
|
| 39 |
+
if not scene_prompt or not scene_prompt.strip():
|
| 40 |
+
return None
|
| 41 |
+
image = client.text_to_image(
|
| 42 |
+
prompt=f"Nobel literary book cover illustration, werewolf lore, cinematic natural lighting, clean painterly style, {scene_prompt}",
|
| 43 |
+
model="black-forest-labs/FLUX.1-schnell",
|
| 44 |
+
)
|
| 45 |
+
return image
|
| 46 |
+
|
| 47 |
+
def generate_book_trailer(video_prompt):
|
| 48 |
+
if not video_prompt or not video_prompt.strip():
|
| 49 |
+
return None
|
| 50 |
+
video_bytes = client.text_to_video(
|
| 51 |
+
prompt=f"Cinematic literary novel book trailer shot, luminous atmosphere, {video_prompt}",
|
| 52 |
+
model="Wan-AI/Wan2.1-T2V-14B",
|
| 53 |
+
)
|
| 54 |
+
output_path = "trailer_clip.mp4"
|
| 55 |
+
with open(output_path, "wb") as f:
|
| 56 |
+
f.write(video_bytes)
|
| 57 |
+
return output_path
|
| 58 |
+
|
| 59 |
+
with gr.Blocks(title="Knowledge AI Studio", analytics_enabled=False) as demo:
|
| 60 |
+
gr.Markdown("# π Knowledge AI Studio")
|
| 61 |
+
gr.Markdown("Private co-writing and visual studio powered by **Ling 3.1 Flash**, **FLUX.1**, and **Wan 2.1**.")
|
| 62 |
+
|
| 63 |
+
with gr.Tab("βοΈ Chapter & Lore Writer"):
|
| 64 |
+
temp_slider = gr.Slider(0.5, 1.2, value=0.9, label="Creativity (Temperature)")
|
| 65 |
+
gr.ChatInterface(
|
| 66 |
+
fn=write_chapter,
|
| 67 |
+
additional_inputs=[temp_slider],
|
| 68 |
+
examples=[
|
| 69 |
+
["Write an opening chapter where an ancient alpha discovers a forgotten treaty under a silver full moon.", 0.9],
|
| 70 |
+
["Create a rich cultural history and lineage for three noble werewolf clans.", 0.85],
|
| 71 |
+
],
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
with gr.Tab("π¨ Book Cover & Character Art"):
|
| 75 |
+
art_input = gr.Textbox(
|
| 76 |
+
label="Describe your character or scene",
|
| 77 |
+
placeholder="Silver-furred werewolf standing on a sunlit mountain ridge overlooking an ancient valley..."
|
| 78 |
+
)
|
| 79 |
+
art_btn = gr.Button("Generate Artwork", variant="primary")
|
| 80 |
+
art_output = gr.Image(label="Generated Book Art")
|
| 81 |
+
art_btn.click(fn=generate_scene_art, inputs=art_input, outputs=art_output)
|
| 82 |
+
|
| 83 |
+
with gr.Tab("π¬ Book Trailer Video"):
|
| 84 |
+
vid_input = gr.Textbox(
|
| 85 |
+
label="Describe the trailer motion scene",
|
| 86 |
+
placeholder="Slow sweeping camera through a misty northern pine forest at dawn..."
|
| 87 |
+
)
|
| 88 |
+
vid_btn = gr.Button("Generate Video Clip", variant="primary")
|
| 89 |
+
vid_output = gr.Video(label="Generated Trailer Clip")
|
| 90 |
+
vid_btn.click(fn=generate_book_trailer, inputs=vid_input, outputs=vid_output)
|
| 91 |
+
|
| 92 |
+
if __name__ == "__main__":
|
| 93 |
+
if APP_PASS:
|
| 94 |
+
demo.launch(
|
| 95 |
+
theme=gr.themes.Soft(),
|
| 96 |
+
auth=(APP_USER, APP_PASS),
|
| 97 |
+
auth_message="π Authorized Family Access Only",
|
| 98 |
+
pwa=True,
|
| 99 |
+
)
|
| 100 |
+
else:
|
| 101 |
+
demo.launch(theme=gr.themes.Soft(), pwa=True)
|