Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,50 +1,68 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from groq import Groq
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
| 5 |
"""
|
| 6 |
-
|
| 7 |
-
|
| 8 |
"""
|
| 9 |
if not api_key:
|
| 10 |
return "Bitte API-Key eingeben!", "<p>Keine Vorschau</p>"
|
| 11 |
-
|
| 12 |
try:
|
| 13 |
client = Groq(api_key=api_key)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
completion = client.chat.completions.create(
|
| 15 |
-
model="
|
| 16 |
-
messages=
|
| 17 |
-
{
|
| 18 |
-
"role": "system",
|
| 19 |
-
"content": (
|
| 20 |
-
"Du bist ein professioneller Game Developer. "
|
| 21 |
-
"Generiere vollständigen HTML5-Spielcode. "
|
| 22 |
-
"Nur HTML + JS, kein Markdown oder Text. "
|
| 23 |
-
"Canvas-basiertes Spiel, sofort spielbar."
|
| 24 |
-
)
|
| 25 |
-
},
|
| 26 |
-
{"role": "user", "content": prompt}
|
| 27 |
-
],
|
| 28 |
temperature=0.8
|
| 29 |
)
|
| 30 |
-
html_code = completion.choices[0].message.content
|
| 31 |
|
| 32 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
preview_html = f"""
|
| 34 |
-
<iframe srcdoc="{
|
| 35 |
width="100%" height="600px" style="border:none;"></iframe>
|
| 36 |
"""
|
| 37 |
-
|
|
|
|
| 38 |
|
| 39 |
except Exception as e:
|
| 40 |
return f"Fehler: {e}", "<p>Fehler bei der Vorschau</p>"
|
| 41 |
|
| 42 |
# Gradio UI
|
| 43 |
with gr.Blocks() as demo:
|
| 44 |
-
gr.Markdown("#
|
| 45 |
gr.Markdown(
|
| 46 |
-
"API-Key eingeben (nicht gespeichert)
|
| 47 |
-
"Die KI generiert HTML5-Code
|
| 48 |
)
|
| 49 |
|
| 50 |
with gr.Row():
|
|
@@ -54,12 +72,12 @@ with gr.Blocks() as demo:
|
|
| 54 |
placeholder="Hier API-Key einfügen",
|
| 55 |
type="password"
|
| 56 |
)
|
| 57 |
-
|
| 58 |
-
label="Spielidee",
|
| 59 |
placeholder="Beispiel: 2D Jump'n'Run mit Gegnern",
|
| 60 |
lines=3
|
| 61 |
)
|
| 62 |
-
btn = gr.Button("🎮
|
| 63 |
code_output = gr.Code(
|
| 64 |
language="html",
|
| 65 |
label="HTML5 Game Code",
|
|
@@ -69,9 +87,7 @@ with gr.Blocks() as demo:
|
|
| 69 |
with gr.Column(scale=1):
|
| 70 |
preview_output = gr.HTML(label="Live Vorschau")
|
| 71 |
|
| 72 |
-
# Event
|
| 73 |
-
btn.click(
|
| 74 |
-
inputs=[api_key_input, prompt_input],
|
| 75 |
-
outputs=[code_output, preview_output])
|
| 76 |
|
| 77 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from groq import Groq
|
| 3 |
|
| 4 |
+
# Speichert die Konversation pro Session
|
| 5 |
+
chat_history = []
|
| 6 |
+
|
| 7 |
+
def chat_game(api_key, user_input):
|
| 8 |
"""
|
| 9 |
+
Chat für HTML5 Game Coding mit Follow-up.
|
| 10 |
+
Gibt Code + Live Preview zurück.
|
| 11 |
"""
|
| 12 |
if not api_key:
|
| 13 |
return "Bitte API-Key eingeben!", "<p>Keine Vorschau</p>"
|
| 14 |
+
|
| 15 |
try:
|
| 16 |
client = Groq(api_key=api_key)
|
| 17 |
+
|
| 18 |
+
# System Prompt definiert die Rolle der KI
|
| 19 |
+
system_prompt = {
|
| 20 |
+
"role": "system",
|
| 21 |
+
"content": (
|
| 22 |
+
"Du bist ein professioneller Game Developer und AI-Coding-Assistent. "
|
| 23 |
+
"Erzeuge oder korrigiere HTML5-Spiele. "
|
| 24 |
+
"Nur gültiger HTML+JS Code, keine Erklärungen. "
|
| 25 |
+
"Canvas-basiertes Spiel, sofort spielbar."
|
| 26 |
+
)
|
| 27 |
+
}
|
| 28 |
+
|
| 29 |
+
# User Prompt hinzufügen
|
| 30 |
+
user_message = {"role": "user", "content": user_input}
|
| 31 |
+
|
| 32 |
+
# Alle vorherigen Messages + neue Nachricht
|
| 33 |
+
messages = [system_prompt] + chat_history + [user_message]
|
| 34 |
+
|
| 35 |
+
# Anfrage an Groq
|
| 36 |
completion = client.chat.completions.create(
|
| 37 |
+
model="llama3-70b-8192", # oder gpt-os wenn verfügbar
|
| 38 |
+
messages=messages,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
temperature=0.8
|
| 40 |
)
|
|
|
|
| 41 |
|
| 42 |
+
# Antwort des Models
|
| 43 |
+
model_response = completion.choices[0].message.content
|
| 44 |
+
|
| 45 |
+
# Antwort zur History hinzufügen
|
| 46 |
+
chat_history.append(user_message)
|
| 47 |
+
chat_history.append({"role": "assistant", "content": model_response})
|
| 48 |
+
|
| 49 |
+
# Vorschau im iframe
|
| 50 |
preview_html = f"""
|
| 51 |
+
<iframe srcdoc="{model_response.replace('"', '"')}"
|
| 52 |
width="100%" height="600px" style="border:none;"></iframe>
|
| 53 |
"""
|
| 54 |
+
|
| 55 |
+
return model_response, preview_html
|
| 56 |
|
| 57 |
except Exception as e:
|
| 58 |
return f"Fehler: {e}", "<p>Fehler bei der Vorschau</p>"
|
| 59 |
|
| 60 |
# Gradio UI
|
| 61 |
with gr.Blocks() as demo:
|
| 62 |
+
gr.Markdown("# 🤖 HTML5 Game AI Chat mit Live Preview")
|
| 63 |
gr.Markdown(
|
| 64 |
+
"API-Key eingeben (nicht gespeichert) und eine Spielidee oder Folgefrage stellen. "
|
| 65 |
+
"Die KI generiert HTML5-Code und zeigt Live-Vorschau rechts."
|
| 66 |
)
|
| 67 |
|
| 68 |
with gr.Row():
|
|
|
|
| 72 |
placeholder="Hier API-Key einfügen",
|
| 73 |
type="password"
|
| 74 |
)
|
| 75 |
+
user_input = gr.Textbox(
|
| 76 |
+
label="Deine Frage / Spielidee",
|
| 77 |
placeholder="Beispiel: 2D Jump'n'Run mit Gegnern",
|
| 78 |
lines=3
|
| 79 |
)
|
| 80 |
+
btn = gr.Button("🎮 Senden")
|
| 81 |
code_output = gr.Code(
|
| 82 |
language="html",
|
| 83 |
label="HTML5 Game Code",
|
|
|
|
| 87 |
with gr.Column(scale=1):
|
| 88 |
preview_output = gr.HTML(label="Live Vorschau")
|
| 89 |
|
| 90 |
+
# Event
|
| 91 |
+
btn.click(chat_game, inputs=[api_key_input, user_input], outputs=[code_output, preview_output])
|
|
|
|
|
|
|
| 92 |
|
| 93 |
demo.launch()
|