Tim13ekd commited on
Commit
16aec25
·
verified ·
1 Parent(s): 1b19da6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -14
app.py CHANGED
@@ -5,6 +5,7 @@ import json
5
  import datetime
6
  import copy
7
 
 
8
  PROJECT_DIR = "projects"
9
  os.makedirs(PROJECT_DIR, exist_ok=True)
10
 
@@ -29,6 +30,9 @@ def create_checkpoint():
29
  }
30
  current_project["checkpoints"].append(checkpoint)
31
 
 
 
 
32
  def new_project():
33
  global current_project
34
  timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
@@ -51,7 +55,7 @@ loop();
51
  "checkpoints": []
52
  }
53
  create_checkpoint()
54
- return current_project["code"], "<p>Live Vorschau</p>", list_projects(), "Neues Projekt erstellt: " + current_project["name"]
55
 
56
  def load_project(project_name):
57
  global current_project
@@ -59,8 +63,11 @@ def load_project(project_name):
59
  if os.path.exists(filepath):
60
  with open(filepath,"r",encoding="utf-8") as f:
61
  current_project = json.load(f)
62
- return current_project["code"], f"<iframe srcdoc='{current_project['code'].replace('\"','&quot;')}' width='100%' height='400px' style='border:none;'></iframe>", list_projects(), f"Projekt geladen: {project_name}", update_checkpoint_list()
63
- return "", "<p>Keine Vorschau</p>", list_projects(), "Projekt nicht gefunden", []
 
 
 
64
 
65
  def save_project():
66
  global current_project
@@ -104,21 +111,21 @@ def chat_generate(api_key, model_name, user_input, current_code):
104
  current_project["chat_history"].append({"role":"assistant","content":model_response})
105
  create_checkpoint()
106
 
107
- preview_html = f"<iframe srcdoc='{model_response.replace('\"','&quot;')}' width='100%' height='400px' style='border:none;'></iframe>"
 
108
 
109
  return model_response, preview_html, "✅ KI Update fertig", update_checkpoint_list()
110
  except Exception as e:
111
  return current_code, "<p>Fehler bei der Vorschau</p>", f"Fehler: {e}", update_checkpoint_list()
112
 
113
- def update_checkpoint_list():
114
- return [f"{c['timestamp']}" for c in current_project["checkpoints"]] if current_project["checkpoints"] else []
115
-
116
  def load_checkpoint(checkpoint_time):
117
  for c in current_project["checkpoints"]:
118
- if c["timestamp"]==checkpoint_time:
119
- current_project["code"]=c["code"]
120
- current_project["chat_history"]=c["chat_history"]
121
- return current_project["code"], f"<iframe srcdoc='{c['code'].replace('\"','&quot;')}' width='100%' height='400px' style='border:none;'></iframe>", "✅ Checkpoint geladen"
 
 
122
  return current_project["code"], "<p>Keine Vorschau</p>", "Checkpoint nicht gefunden"
123
 
124
  def delete_checkpoint(checkpoint_time):
@@ -136,8 +143,9 @@ with gr.Blocks() as demo:
136
  with gr.Column(scale=1):
137
  gr.Markdown("### KI Beschreibung / Hinweise")
138
  ki_desc = gr.HTML("<p>Hier erscheinen Hinweise der KI und aktuelle Anfrage</p>")
 
139
  api_key_input = gr.Textbox(label="GROQ API-Key", placeholder="Hier API-Key einfügen", type="password")
140
- model_dropdown = gr.Dropdown(choices=["llama3-70b-8192","gpt-os"], value="llama3-70b-8192", label="AI Modell auswählen")
141
  user_input = gr.Textbox(label="Spielidee / Follow-up", placeholder="Beispiel: Gegner hinzufügen", lines=3)
142
 
143
  with gr.Row():
@@ -164,11 +172,11 @@ with gr.Blocks() as demo:
164
  # ------------------------------
165
  # Events
166
  # ------------------------------
167
- btn_new.click(new_project, outputs=[code_editor, preview_output, project_dropdown, status_output])
168
  btn_load.click(load_project, inputs=[project_dropdown], outputs=[code_editor, preview_output, project_dropdown, status_output, checkpoint_dropdown])
169
  btn_save_code.click(save_code_local, inputs=[code_editor], outputs=[status_output, checkpoint_dropdown])
170
  btn_save_project.click(save_project, outputs=[status_output])
171
- btn_ai.click(chat_generate, inputs=[api_key_input, model_dropdown, user_input, code_editor], outputs=[code_editor, preview_output, status_output, checkpoint_dropdown])
172
  btn_load_checkpoint.click(load_checkpoint, inputs=[checkpoint_dropdown], outputs=[code_editor, preview_output, status_output])
173
  btn_delete_checkpoint.click(delete_checkpoint, inputs=[checkpoint_dropdown], outputs=[checkpoint_dropdown, status_output])
174
 
 
5
  import datetime
6
  import copy
7
 
8
+ # Ordner für lokale Projekte
9
  PROJECT_DIR = "projects"
10
  os.makedirs(PROJECT_DIR, exist_ok=True)
11
 
 
30
  }
31
  current_project["checkpoints"].append(checkpoint)
32
 
33
+ def update_checkpoint_list():
34
+ return [c["timestamp"] for c in current_project["checkpoints"]] if current_project["checkpoints"] else []
35
+
36
  def new_project():
37
  global current_project
38
  timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
 
55
  "checkpoints": []
56
  }
57
  create_checkpoint()
58
+ return current_project["code"], "<p>Live Vorschau</p>", list_projects(), "Neues Projekt erstellt: " + current_project["name"], update_checkpoint_list()
59
 
60
  def load_project(project_name):
61
  global current_project
 
63
  if os.path.exists(filepath):
64
  with open(filepath,"r",encoding="utf-8") as f:
65
  current_project = json.load(f)
66
+ html_escaped = current_project["code"].replace('"', "&quot;")
67
+ iframe_html = f"<iframe srcdoc='{html_escaped}' width='100%' height='400px' style='border:none;'></iframe>"
68
+ return current_project["code"], iframe_html, list_projects(), f"Projekt geladen: {project_name}", update_checkpoint_list()
69
+ else:
70
+ return "", "<p>Keine Vorschau</p>", list_projects(), "Projekt nicht gefunden", []
71
 
72
  def save_project():
73
  global current_project
 
111
  current_project["chat_history"].append({"role":"assistant","content":model_response})
112
  create_checkpoint()
113
 
114
+ html_escaped = model_response.replace('"', "&quot;")
115
+ preview_html = f"<iframe srcdoc='{html_escaped}' width='100%' height='400px' style='border:none;'></iframe>"
116
 
117
  return model_response, preview_html, "✅ KI Update fertig", update_checkpoint_list()
118
  except Exception as e:
119
  return current_code, "<p>Fehler bei der Vorschau</p>", f"Fehler: {e}", update_checkpoint_list()
120
 
 
 
 
121
  def load_checkpoint(checkpoint_time):
122
  for c in current_project["checkpoints"]:
123
+ if c["timestamp"] == checkpoint_time:
124
+ current_project["code"] = c["code"]
125
+ current_project["chat_history"] = c["chat_history"]
126
+ html_escaped = c["code"].replace('"', "&quot;")
127
+ iframe_html = f"<iframe srcdoc='{html_escaped}' width='100%' height='400px' style='border:none;'></iframe>"
128
+ return current_project["code"], iframe_html, "✅ Checkpoint geladen"
129
  return current_project["code"], "<p>Keine Vorschau</p>", "Checkpoint nicht gefunden"
130
 
131
  def delete_checkpoint(checkpoint_time):
 
143
  with gr.Column(scale=1):
144
  gr.Markdown("### KI Beschreibung / Hinweise")
145
  ki_desc = gr.HTML("<p>Hier erscheinen Hinweise der KI und aktuelle Anfrage</p>")
146
+
147
  api_key_input = gr.Textbox(label="GROQ API-Key", placeholder="Hier API-Key einfügen", type="password")
148
+ model_input = gr.Textbox(label="AI Modell", placeholder="Beispiel: llama3-70b-8192 oder gpt-os", value="llama3-70b-8192")
149
  user_input = gr.Textbox(label="Spielidee / Follow-up", placeholder="Beispiel: Gegner hinzufügen", lines=3)
150
 
151
  with gr.Row():
 
172
  # ------------------------------
173
  # Events
174
  # ------------------------------
175
+ btn_new.click(new_project, outputs=[code_editor, preview_output, project_dropdown, status_output, checkpoint_dropdown])
176
  btn_load.click(load_project, inputs=[project_dropdown], outputs=[code_editor, preview_output, project_dropdown, status_output, checkpoint_dropdown])
177
  btn_save_code.click(save_code_local, inputs=[code_editor], outputs=[status_output, checkpoint_dropdown])
178
  btn_save_project.click(save_project, outputs=[status_output])
179
+ btn_ai.click(chat_generate, inputs=[api_key_input, model_input, user_input, code_editor], outputs=[code_editor, preview_output, status_output, checkpoint_dropdown])
180
  btn_load_checkpoint.click(load_checkpoint, inputs=[checkpoint_dropdown], outputs=[code_editor, preview_output, status_output])
181
  btn_delete_checkpoint.click(delete_checkpoint, inputs=[checkpoint_dropdown], outputs=[checkpoint_dropdown, status_output])
182