Cristobal299 commited on
Commit
83207a8
·
verified ·
1 Parent(s): f3a3f2c

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +70 -12
app.py CHANGED
@@ -1,6 +1,10 @@
1
  # -*- coding: utf-8 -*-
2
  import os
3
  import warnings
 
 
 
 
4
 
5
  import gradio as gr
6
 
@@ -26,7 +30,7 @@ else:
26
  print("GROQ_API_KEY not set. The app will work in demo mode with placeholder messages.")
27
 
28
  # ----------------------------------------------------------------------
29
- # Helper: list of supported languages (common set)
30
  # ----------------------------------------------------------------------
31
  SUPPORTED_LANGUAGES = [
32
  "Python",
@@ -55,7 +59,6 @@ SUPPORTED_LANGUAGES = [
55
  def improve_code(original_code: str, language: str) -> str:
56
  """
57
  Sends the original code to the LLM and returns an improved version.
58
- The language selector is used to guide the model.
59
  """
60
  if not original_code.strip():
61
  return "Please provide some code to improve."
@@ -83,24 +86,49 @@ def improve_code(original_code: str, language: str) -> str:
83
  return f"Error communicating with Groq: {e}"
84
 
85
  # ----------------------------------------------------------------------
86
- # Function: generate full web code from an idea
87
  # ----------------------------------------------------------------------
88
- def generate_web_code(idea: str, language: str) -> str:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  """
90
  Generates a complete web project (HTML/CSS/JS or a single‑file script)
91
  based on the user's idea and the selected language.
 
92
  """
93
  if not idea.strip():
94
- return "Please describe the web idea you want to build."
95
 
96
  if not groq_client:
97
- return "No Groq API key configured. Set the GROQ_API_KEY environment variable."
98
 
99
  prompt = (
100
  f"You are a senior full‑stack developer. Based on the following description, "
101
  f"write a complete, runnable {language} web application. Include all necessary "
102
  "files (HTML, CSS, JavaScript, or a single script) and comment the code. "
103
- "Return the code only, without extra explanation.\n\n"
 
 
 
 
 
 
104
  f"Description:\n{idea}"
105
  )
106
 
@@ -112,9 +140,34 @@ def generate_web_code(idea: str, language: str) -> str:
112
  max_tokens=4000,
113
  )
114
  generated = response.choices[0].message.content
115
- return generated.strip() if generated else "No content returned from Groq."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
116
  except Exception as e:
117
- return f"Error communicating with Groq: {e}"
118
 
119
  # ----------------------------------------------------------------------
120
  # Gradio UI
@@ -169,16 +222,21 @@ with gr.Blocks() as demo:
169
  lines=12,
170
  )
171
  with gr.Column():
172
- generated_output = gr.Textbox(
173
- label="Generated Code",
174
  placeholder="The generated code will appear here",
175
  lines=12,
176
  )
 
 
 
 
 
177
  generate_btn = gr.Button("Generate")
178
  generate_btn.click(
179
  fn=generate_web_code,
180
  inputs=[idea_input, lang_select_generate],
181
- outputs=generated_output,
182
  )
183
 
184
  demo.launch()
 
1
  # -*- coding: utf-8 -*-
2
  import os
3
  import warnings
4
+ import io
5
+ import zipfile
6
+ import tempfile
7
+ from pathlib import Path
8
 
9
  import gradio as gr
10
 
 
30
  print("GROQ_API_KEY not set. The app will work in demo mode with placeholder messages.")
31
 
32
  # ----------------------------------------------------------------------
33
+ # Supported languages (common set)
34
  # ----------------------------------------------------------------------
35
  SUPPORTED_LANGUAGES = [
36
  "Python",
 
59
  def improve_code(original_code: str, language: str) -> str:
60
  """
61
  Sends the original code to the LLM and returns an improved version.
 
62
  """
63
  if not original_code.strip():
64
  return "Please provide some code to improve."
 
86
  return f"Error communicating with Groq: {e}"
87
 
88
  # ----------------------------------------------------------------------
89
+ # Helper: create a zip file with given files
90
  # ----------------------------------------------------------------------
91
+ def _create_zip(file_dict: dict) -> str:
92
+ """
93
+ file_dict: mapping of relative file path -> file content (string)
94
+ Returns the path to the created zip file.
95
+ """
96
+ tmp_dir = tempfile.mkdtemp()
97
+ zip_path = Path(tmp_dir) / "generated_project.zip"
98
+
99
+ with zipfile.ZipFile(zip_path, "w", zipfile.ZIP_DEFLATED) as zipf:
100
+ for rel_path, content in file_dict.items():
101
+ # Ensure parent directories exist inside the zip
102
+ zipf.writestr(rel_path, content)
103
+
104
+ return str(zip_path)
105
+
106
+ # ----------------------------------------------------------------------
107
+ # Function: generate full web project from an idea
108
+ # ----------------------------------------------------------------------
109
+ def generate_web_code(idea: str, language: str):
110
  """
111
  Generates a complete web project (HTML/CSS/JS or a single‑file script)
112
  based on the user's idea and the selected language.
113
+ Returns a tuple: (preview_text, zip_file_path)
114
  """
115
  if not idea.strip():
116
+ return ("Please describe the web idea you want to build.", None)
117
 
118
  if not groq_client:
119
+ return ("No Groq API key configured. Set the GROQ_API_KEY environment variable.", None)
120
 
121
  prompt = (
122
  f"You are a senior full‑stack developer. Based on the following description, "
123
  f"write a complete, runnable {language} web application. Include all necessary "
124
  "files (HTML, CSS, JavaScript, or a single script) and comment the code. "
125
+ "Also provide a minimal requirements.txt, a README.md explaining how to run the project, "
126
+ "and a Dockerfile that builds and runs the app. Return the files in the following format:\n"
127
+ "=== filename ===\n"
128
+ "<file content>\n"
129
+ "=== filename ===\n"
130
+ "<file content>\n"
131
+ "Do not add any extra explanation.\n\n"
132
  f"Description:\n{idea}"
133
  )
134
 
 
140
  max_tokens=4000,
141
  )
142
  generated = response.choices[0].message.content
143
+ if not generated:
144
+ return ("No content returned from Groq.", None)
145
+
146
+ # Parse the generated sections
147
+ sections = {}
148
+ current_file = None
149
+ for line in generated.splitlines():
150
+ if line.startswith("=== ") and line.endswith(" ==="):
151
+ current_file = line.strip("= ").strip()
152
+ sections[current_file] = ""
153
+ elif current_file:
154
+ sections[current_file] += line + "\n"
155
+
156
+ # Ensure we have at least a main file for preview
157
+ preview_key = None
158
+ for key in sections:
159
+ if key.lower().endswith((".html", ".py", ".js")):
160
+ preview_key = key
161
+ break
162
+ preview_text = sections.get(preview_key, "Generated files:\n" + "\n".join(sections.keys()))
163
+
164
+ # Create zip with all files
165
+ zip_path = _create_zip(sections)
166
+
167
+ return (preview_text, zip_path)
168
+
169
  except Exception as e:
170
+ return (f"Error communicating with Groq: {e}", None)
171
 
172
  # ----------------------------------------------------------------------
173
  # Gradio UI
 
222
  lines=12,
223
  )
224
  with gr.Column():
225
+ generated_preview = gr.Textbox(
226
+ label="Generated Files Preview",
227
  placeholder="The generated code will appear here",
228
  lines=12,
229
  )
230
+ download_btn = gr.File(
231
+ label="Download Project Zip",
232
+ type="file",
233
+ visible=False,
234
+ )
235
  generate_btn = gr.Button("Generate")
236
  generate_btn.click(
237
  fn=generate_web_code,
238
  inputs=[idea_input, lang_select_generate],
239
+ outputs=[generated_preview, download_btn],
240
  )
241
 
242
  demo.launch()