acecalisto3 commited on
Commit
b62e04c
·
verified ·
1 Parent(s): b4cfb7e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -14
app.py CHANGED
@@ -101,7 +101,7 @@ def execute_pip_command(command, add_message):
101
  break
102
  if output:
103
  # Corrected line: Combine the f-string parts
104
- add_message("System", f"'"{output.strip()}"'")
105
  time.sleep(0.1) # Simulate delay for more realistic streaming
106
  rc = process.poll()
107
  return rc
@@ -143,7 +143,7 @@ def generate_mini_app_ideas(theme):
143
 
144
  def generate_app_code(app_name, app_description, model_name, history):
145
  """Generates code for the selected mini-app using the specified GGUF model."""
146
- prompt = f"'"Write a Python script for a {app_description} named {app_name} using Gradio and Streamlit:"'"
147
  agent = get_agent(model_name)
148
  generated_code = agent.chat(prompt, history)
149
  return generated_code
@@ -158,11 +158,11 @@ def execute_terminal_command(command):
158
 
159
  def install_package(package_name):
160
  """Installs a package using pip."""
161
- output, error = execute_terminal_command(f"'"pip install {package_name}"'")
162
  if error:
163
- return f"'"Error installing package: {error}"'"
164
  else:
165
- return f"'"Package `{package_name}` installed successfully."'"
166
 
167
  def get_project_data():
168
  """Returns the current project data."""
@@ -181,7 +181,7 @@ def handle_chat(input_text, history):
181
 
182
  if input_text.startswith("pip install ") or input_text.startswith("https://pypi.org/project/"):
183
  package_name = extract_package_name(input_text)
184
- add_message("System", f"'"Installing `{package_name}`...")"'"
185
  result = install_package(package_name)
186
  add_message("System", result)
187
  update_project_data("packages", CURRENT_PROJECT.get("packages", []) + [package_name])
@@ -194,15 +194,18 @@ def handle_chat(input_text, history):
194
  elif not MINI_APPS:
195
  add_message("System", "Here are some ideas:")
196
  for idea in generate_mini_app_ideas(input_text):
197
- add_message("System", f"'"- {idea}"'")
198
  add_message("System", "Which one would you like to build?")
199
  elif CURRENT_APP["name"] is None:
200
  selected_app = input_text
201
  app_description = next((app for app in MINI_APPS if selected_app in app), None)
202
  if app_description:
203
- add_message("System", f"'"Generating code for {app_description}..."'")
204
  code = generate_app_code(selected_app, app_description, "CodeQwen", history) # Use CodeQwen by default
205
- add_message("System", f"'"python {code}
 
 
 
206
 
207
  add_message("System", "Code generated! What else can I do for you?")
208
  update_project_data("code", code)
@@ -219,7 +222,7 @@ add_message("System", "Code generated! What else can I do for you?")
219
  def generate_code_tool(input_text, history):
220
  """Prebuilt tool for code generation."""
221
  code = generate_app_code("MyTool", "A tool to do something", "CodeQwen", history) # Use CodeQwen by default
222
- return f"'"python\n{code}\n"'"
223
 
224
  def analyze_code_tool(input_text, history):
225
  """Prebuilt tool for code analysis."""
@@ -241,7 +244,7 @@ chat_input = st.text_input("Tell me your idea...", key="chat_input")
241
  if chat_input:
242
  chat_history, dynamic_functions = handle_chat(chat_input, chat_history)
243
  for sender, message in chat_history:
244
- st.markdown(f"'"**{sender}:** {message}"'")
245
 
246
  # --- Code Execution and Deployment ---
247
  if CURRENT_APP["code"]:
@@ -269,13 +272,16 @@ if CURRENT_APP["code"]:
269
  if st.button("Edit Code"):
270
  try:
271
  # Use Hugging Face's text-generation pipeline for code editing
272
- prompt = f"'"Improve the following Python code: python {code_area}"'"
273
 
274
  inputs = tokenizer(prompt, return_tensors="pt")
275
  output = model.generate(**inputs, max_length=500, num_return_sequences=1)
276
- edited_code = tokenizer.decode(output[0], skip_special_tokens=True).split("[python [1].split("
 
 
 
277
 
278
- st.success(f"'"Code edited successfully!\n{edited_code}"'"")
279
  update_project_data("code", edited_code)
280
  code_area.value = edited_code
281
  except Exception as e:
 
101
  break
102
  if output:
103
  # Corrected line: Combine the f-string parts
104
+ add_message("System", f"'"{output.strip()}")
105
  time.sleep(0.1) # Simulate delay for more realistic streaming
106
  rc = process.poll()
107
  return rc
 
143
 
144
  def generate_app_code(app_name, app_description, model_name, history):
145
  """Generates code for the selected mini-app using the specified GGUF model."""
146
+ prompt = f"'"Write a Python script for a {app_description} named {app_name} using Gradio and Streamlit:"
147
  agent = get_agent(model_name)
148
  generated_code = agent.chat(prompt, history)
149
  return generated_code
 
158
 
159
  def install_package(package_name):
160
  """Installs a package using pip."""
161
+ output, error = execute_terminal_command(f"'"pip install {package_name}")
162
  if error:
163
+ return f"'"Error installing package: {error}"
164
  else:
165
+ return f"'"Package `{package_name}` installed successfully."
166
 
167
  def get_project_data():
168
  """Returns the current project data."""
 
181
 
182
  if input_text.startswith("pip install ") or input_text.startswith("https://pypi.org/project/"):
183
  package_name = extract_package_name(input_text)
184
+ add_message("System", f"'"Installing `{package_name}`...")
185
  result = install_package(package_name)
186
  add_message("System", result)
187
  update_project_data("packages", CURRENT_PROJECT.get("packages", []) + [package_name])
 
194
  elif not MINI_APPS:
195
  add_message("System", "Here are some ideas:")
196
  for idea in generate_mini_app_ideas(input_text):
197
+ add_message("System", f"'"-{idea}")
198
  add_message("System", "Which one would you like to build?")
199
  elif CURRENT_APP["name"] is None:
200
  selected_app = input_text
201
  app_description = next((app for app in MINI_APPS if selected_app in app), None)
202
  if app_description:
203
+ add_message("System", f"'"Generating code for {app_description}...")
204
  code = generate_app_code(selected_app, app_description, "CodeQwen", history) # Use CodeQwen by default
205
+ add_message("System", f"'"
206
+
207
+
208
+ python\n{code}\n
209
 
210
  add_message("System", "Code generated! What else can I do for you?")
211
  update_project_data("code", code)
 
222
  def generate_code_tool(input_text, history):
223
  """Prebuilt tool for code generation."""
224
  code = generate_app_code("MyTool", "A tool to do something", "CodeQwen", history) # Use CodeQwen by default
225
+ return f"'" python {code}"
226
 
227
  def analyze_code_tool(input_text, history):
228
  """Prebuilt tool for code analysis."""
 
244
  if chat_input:
245
  chat_history, dynamic_functions = handle_chat(chat_input, chat_history)
246
  for sender, message in chat_history:
247
+ st.markdown(f"**{sender}:** {message}")
248
 
249
  # --- Code Execution and Deployment ---
250
  if CURRENT_APP["code"]:
 
272
  if st.button("Edit Code"):
273
  try:
274
  # Use Hugging Face's text-generation pipeline for code editing
275
+ prompt = f"'"Improve the following Python code: python {code_area}"
276
 
277
  inputs = tokenizer(prompt, return_tensors="pt")
278
  output = model.generate(**inputs, max_length=500, num_return_sequences=1)
279
+ edited_code = tokenizer.decode(output[0], skip_special_tokens=True).split("
280
+
281
+
282
+ python\n")[1].split("\n
283
 
284
+ st.success(f"'"Code edited successfully!\n{edited_code}")
285
  update_project_data("code", edited_code)
286
  code_area.value = edited_code
287
  except Exception as e: