| import os |
| import gradio as gr |
| from huggingface_hub import InferenceClient |
|
|
| def generate_novel_chapter(genre, style, premise, tone, length_tokens): |
| if not premise.strip(): |
| raise gr.Error("Bitte gib eine Handlungsvorgabe ein.") |
|
|
| HF_TOKEN = os.getenv("HF_TOKEN") |
| client = InferenceClient(token=HF_TOKEN) if HF_TOKEN else InferenceClient() |
|
|
| system_instruction = ( |
| f"Du bist ein preisgekrönter Romanautor. Genre: {genre}. Stil: {style}. Ton: {tone}. " |
| "WICHTIGE ANWEISUNG: Schreibe das gesamte Kapitel AUSSCHLIESSLICH auf DEUTSCH." |
| ) |
|
|
| |
| stream = client.chat_completion( |
| model="Qwen/Qwen2.5-72B-Instruct", |
| messages=[ |
| {"role": "system", "content": system_instruction}, |
| {"role": "user", "content": f"Schreibe ein detailliertes Kapitel basierend auf: {premise}"} |
| ], |
| max_tokens=int(length_tokens), |
| temperature=0.7, |
| stream=True |
| ) |
|
|
| full_text = "" |
| for chunk in stream: |
| if chunk.choices[0].delta.content: |
| full_text += chunk.choices[0].delta.content |
| yield full_text |
|
|
| |
| with gr.Blocks(theme=gr.themes.Default()) as demo: |
| gr.Markdown("# 📚 GamerJam.de AI Story Studio") |
| |
| with gr.Row(): |
| with gr.Column(scale=1): |
| genre = gr.Dropdown(["High Fantasy", "Sci-Fi", "Cyberpunk", "Abenteuer", "Drama", "Krimi", "Thriller"], label="Genre", value="High Fantasy") |
| style = gr.Dropdown(["Erzähler (3. Person)", "Ich-Perspektive"], label="Erzählstil") |
| tone = gr.Dropdown(["Düster", "Episch", "Spannend", "Poetisch", "Humorvoll"], label="Tonalität") |
| length = gr.Slider(500, 2500, value=1200, label="Tokens") |
| with gr.Column(scale=2): |
| premise = gr.Textbox(label="Handlungsvorgabe", lines=8) |
| submit_btn = gr.Button("Kapitel generieren", variant="primary") |
| |
| output = gr.Markdown(label="Dein Kapitel") |
| |
| submit_btn.click( |
| fn=generate_novel_chapter, |
| inputs=[genre, style, premise, tone, length], |
| outputs=output |
| ) |
|
|
| if __name__ == "__main__": |
| demo.launch() |