acecalisto3 commited on
Commit
c3b5b15
1 Parent(s): a1da238

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -89
app.py CHANGED
@@ -9,25 +9,18 @@ import docker
9
  from huggingface_hub import HfApi, create_repo
10
  import importlib
11
  import os
12
- from huggingface_hub import HfApi, create_repo
13
  from transformers import AutoModelForSequenceClassification, pipeline
14
  import huggingface_cli
15
 
16
- model = AutoModelForSequenceClassification.from_pretrained("EleutherAI/code-davinci-002")
17
- codex_pipeline = pipeline("code-generation", model="EleutherAI/code-davinci-002")
18
-
19
- hf_api = HfApi()
20
-
21
  # Initialize Flask app
22
  app = Flask(__name__)
23
- app.config['SECRET_KEY'] = 'your-secret-key'
24
  app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.db'
25
  db = SQLAlchemy(app)
26
  login_manager = LoginManager()
27
  login_manager.init_app(app)
28
 
29
- # User and Project models (as defined earlier)
30
-
31
  class User(UserMixin, db.Model):
32
  id = db.Column(db.Integer, primary_key=True)
33
  username = db.Column(db.String(100), unique=True, nullable=False)
@@ -43,8 +36,7 @@ class Project(db.Model):
43
  def load_user(user_id):
44
  return User.query.get(int(user_id))
45
 
46
- # Authentication routes (as defined earlier)
47
-
48
  @app.route('/register', methods=['POST'])
49
  def register():
50
  data = request.get_json()
@@ -114,7 +106,6 @@ class PluginManager:
114
  return list(self.plugins.keys())
115
 
116
  # Example plugin
117
- # save this as a .py file in your plugin directory
118
  def register_plugin():
119
  return ExamplePlugin()
120
 
@@ -127,6 +118,61 @@ class ExamplePlugin:
127
  plugin_manager = PluginManager('./plugins')
128
  plugin_manager.load_plugins()
129
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
130
  def main():
131
  st.sidebar.title("AI-Guided Development")
132
  app_mode = st.sidebar.selectbox("Choose the app mode",
@@ -225,7 +271,13 @@ def build_and_deploy_page():
225
  @login_required
226
  def ai_assistant_page():
227
  st.header("AI Assistant")
228
- # AI assistant code (as before)
 
 
 
 
 
 
229
 
230
  @login_required
231
  def plugins_page():
@@ -293,82 +345,6 @@ def run_docker_container(image_name, port):
293
  container = client.containers.run(image_name, detach=True, ports={f'{port}/tcp': port})
294
  return container
295
 
296
- def generate_app(user_idea, project_name):
297
- # Extract key information from the user idea
298
- summary = nlp_pipeline(user_idea, max_length=50, min_length=10)[0]["summary_text"]
299
-
300
- # Create project directory if it doesn't exist
301
- project_path = create_project(project_name)
302
-
303
- # Generate code using Codex
304
- prompt = f"""Create a simple Streamlit app for the project named '{project_name}'. The app should display the following summary: '{summary}'."""
305
- generated_code = codex_pipeline(prompt)[0]['generated_text']
306
-
307
- # Save the generated code to a file in the project directory
308
- with open(os.path.join(project_path, "app.py"), "w") as f:
309
- f"""write(generated_code)"""
310
-
311
- # Deploy the app to Hugging Face Spaces
312
- deploy_app_to_hf_spaces(project_name, token, generated_code)
313
-
314
- return generated_code, project_path
315
-
316
- def deploy_app_to_hf_spaces(project_name, token, generated_code):
317
- repo_name = f"""hf-{project_name}"""
318
- repo_id = hf_api.changelog.get_repo_id(repo_name)
319
-
320
- if not repo_id:
321
- create_repo = huggingface_cli.create_repo(repo_name, "public", "Streamlit App", token)
322
- repo_id = create_repo["repo_id"]
323
-
324
- # Save the generated code to a temporary file
325
-
326
- temp_file = "temp_code.py"
327
- with open(temp_file, "w") as f:
328
- f"""write(generated_code)"""
329
-
330
- # Upload the file to Hugging Face Spaces
331
- api.upload_files(repo_id, [temp_file], token)
332
-
333
- # Delete the temporary file
334
- os.remove(temp_file)
335
-
336
- def launch_chatapp(project_path):
337
- if st.button("Launch ChatApp"):
338
- st.write("Launching ChatApp...")
339
- os.chdir(project_path)
340
- subprocess.run(["python", "app.py"])
341
- st.write("ChatApp launched successfully!")
342
-
343
- def generate_app(user_idea, project_name):
344
- # Extract key information from the user idea
345
- summary = nlp_pipeline(user_idea, max_length=50, min_length=10)[0]["summary_text"]
346
-
347
- # Create project directory if it doesn't exist
348
- project_path = create_project(project_name)
349
-
350
- # Generate code using Codex
351
- prompt = f"Create a simple Streamlit app for the project named '{project_name}'. The app should display the following summary: '{summary}'."
352
- generated_code = codex_pipeline(prompt)[0]['generated_text']
353
-
354
- # Save the generated code to a file in the project directory
355
- with open(os.path.join(project_path, "app.py"), "w") as f:
356
- f"""write(generated_code)"""
357
-
358
- # Upload the file to Hugging Face Spaces
359
- api = HfApi()
360
- repo_id = create_repo(api, project_name)["repo_id"]
361
- temp_file = "temp_code.py"
362
- with open(temp_file, "w") as f:
363
- f"""write(generated_code)"""
364
- api.upload_files(repo_id, [temp_file], api.api_key)
365
-
366
- # Delete the temporary file
367
- os.remove(temp_file)
368
-
369
- # Launch the app
370
- launch_chatapp(project_path)
371
-
372
  if __name__ == "__main__":
373
  db.create_all() # Create the database tables if they don't exist
374
  main()
 
9
  from huggingface_hub import HfApi, create_repo
10
  import importlib
11
  import os
 
12
  from transformers import AutoModelForSequenceClassification, pipeline
13
  import huggingface_cli
14
 
 
 
 
 
 
15
  # Initialize Flask app
16
  app = Flask(__name__)
17
+ app.config['SECRET_KEY'] = 'your-secret-key' # Replace with a strong secret key
18
  app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///users.db'
19
  db = SQLAlchemy(app)
20
  login_manager = LoginManager()
21
  login_manager.init_app(app)
22
 
23
+ # User and Project models
 
24
  class User(UserMixin, db.Model):
25
  id = db.Column(db.Integer, primary_key=True)
26
  username = db.Column(db.String(100), unique=True, nullable=False)
 
36
  def load_user(user_id):
37
  return User.query.get(int(user_id))
38
 
39
+ # Authentication routes
 
40
  @app.route('/register', methods=['POST'])
41
  def register():
42
  data = request.get_json()
 
106
  return list(self.plugins.keys())
107
 
108
  # Example plugin
 
109
  def register_plugin():
110
  return ExamplePlugin()
111
 
 
118
  plugin_manager = PluginManager('./plugins')
119
  plugin_manager.load_plugins()
120
 
121
+ # AI Assistant
122
+ model = AutoModelForSequenceClassification.from_pretrained("EleutherAI/code-davinci-002")
123
+ codex_pipeline = pipeline("code-generation", model=model)
124
+
125
+ hf_api = HfApi()
126
+
127
+ def generate_app(user_idea, project_name):
128
+ # Extract key information from the user idea
129
+ # (You might want to use a more sophisticated NLP pipeline here)
130
+ summary = user_idea # For now, just use the user's input
131
+
132
+ # Create project directory if it doesn't exist
133
+ project_path = create_project(project_name)
134
+
135
+ # Generate code using Codex
136
+ prompt = f"""Create a simple Streamlit app for the project named '{project_name}'. The app should display the following summary: '{summary}'."""
137
+ generated_code = codex_pipeline(prompt)[0]['generated_text']
138
+
139
+ # Save the generated code to a file in the project directory
140
+ with open(os.path.join(project_path, "app.py"), "w") as f:
141
+ f.write(generated_code)
142
+
143
+ # Deploy the app to Hugging Face Spaces
144
+ deploy_app_to_hf_spaces(project_name, generated_code)
145
+
146
+ return generated_code, project_path
147
+
148
+ def deploy_app_to_hf_spaces(project_name, generated_code):
149
+ repo_name = f"hf-{project_name}"
150
+ repo_id = hf_api.changelog.get_repo_id(repo_name)
151
+
152
+ if not repo_id:
153
+ create_repo(hf_api, repo_name, "public", "Streamlit App")
154
+ repo_id = hf_api.changelog.get_repo_id(repo_name)
155
+
156
+ # Save the generated code to a temporary file
157
+ temp_file = "temp_code.py"
158
+ with open(temp_file, "w") as f:
159
+ f.write(generated_code)
160
+
161
+ # Upload the file to Hugging Face Spaces
162
+ hf_api.upload_files(repo_id, [temp_file], hf_api.api_key)
163
+
164
+ # Delete the temporary file
165
+ os.remove(temp_file)
166
+
167
+ # Print success message
168
+ st.write(f"App deployed successfully to Hugging Face Spaces: https://huggingface.co/spaces/{repo_name}")
169
+
170
+ def create_project(project_name):
171
+ project_path = os.path.join(os.getcwd(), project_name)
172
+ if not os.path.exists(project_path):
173
+ os.makedirs(project_path)
174
+ return project_path
175
+
176
  def main():
177
  st.sidebar.title("AI-Guided Development")
178
  app_mode = st.sidebar.selectbox("Choose the app mode",
 
271
  @login_required
272
  def ai_assistant_page():
273
  st.header("AI Assistant")
274
+ user_idea = st.text_area("Describe your app idea:")
275
+ project_name = st.text_input("Enter project name:")
276
+
277
+ if st.button("Generate App"):
278
+ generated_code, project_path = generate_app(user_idea, project_name)
279
+ st.code(generated_code)
280
+ st.write(f"Project directory: {project_path}")
281
 
282
  @login_required
283
  def plugins_page():
 
345
  container = client.containers.run(image_name, detach=True, ports={f'{port}/tcp': port})
346
  return container
347
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
348
  if __name__ == "__main__":
349
  db.create_all() # Create the database tables if they don't exist
350
  main()