acecalisto3 commited on
Commit
535c246
·
verified ·
1 Parent(s): 2bb5759

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +141 -169
app.py CHANGED
@@ -8,7 +8,7 @@ import logging
8
 
9
  import gradio as gr
10
  from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
11
- from huggingface_hub import InferenceClient, cached_download, Repository, HfApi
12
  from IPython.display import display, HTML
13
  import streamlit.components.v1 as components
14
 
@@ -48,7 +48,7 @@ def load_model(model_name: str):
48
 
49
  # Fetch and store the model description
50
  api = HfApi()
51
- model_info = api.model_info(model_name)
52
  model_descriptions[model_name] = model_info.pipeline_tag
53
  return f"Successfully loaded model: {model_name}"
54
  except Exception as e:
@@ -59,7 +59,7 @@ def model_selection():
59
  st.write("Select a model to use for code generation:")
60
  models = ["distilbert", "t5", "codellama-7b", "geminai-1.5b"]
61
  selected_model = st.selectbox("Select a model:", models)
62
- if selected_model:
63
  model = load_model(selected_model)
64
  if model:
65
  st.write(f"Model {selected_model} imported successfully!")
@@ -72,65 +72,56 @@ def run_command(command: str, project_path: str = None) -> str:
72
  """Executes a shell command and returns the output."""
73
  try:
74
  if project_path:
75
- process = subprocess.Popen(command, shell=True, cwd=project_path, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
76
- else:
77
  process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
78
  output, error = process.communicate()
79
  if error:
80
  return f"Error: {error.decode('utf-8')}"
81
- return output.decode("utf-8")
82
  except Exception as e:
83
- return f"Error executing command: {str(e)}"
84
-
85
- def create_project(project_name: str, project_path: str = DEFAULT_PROJECT_PATH):
86
  """Creates a new Hugging Face project."""
87
  global repo
88
- try:
89
- if os.path.exists(project_path):
90
  return f"Error: Directory '{project_path}' already exists!"
91
  # Create the repository
92
  repo = Repository(local_dir=project_path, clone_from=None)
93
  repo.git_init()
94
 
95
- # Add basic files (optional, you can customize this)
96
- with open(os.path.join(project_path, "README.md"), "w") as f:
97
- f.write(f"# {project_name}\n\nA new Hugging Face project.")
98
 
99
- # Stage all changes
100
- repo.git_add(pattern="*")
101
  repo.git_commit(commit_message="Initial commit")
102
 
103
- return f"Hugging Face project '{project_name}' created successfully at '{project_path}'"
104
  except Exception as e:
105
  return f"Error creating Hugging Face project: {str(e)}"
106
 
107
- def list_files(project_path: str = DEFAULT_PROJECT_PATH) -> str:
108
  """Lists files in the project directory."""
109
  try:
110
  files = os.listdir(project_path)
111
  if not files:
112
  return "Project directory is empty."
113
  return "\n".join(files)
114
- except Exception as e:
115
- return f"Error listing project files: {str(e)}"
116
 
117
- def read_file(file_path: str, project_path: str = DEFAULT_PROJECT_PATH) -> str:
118
- """Reads and returns the content of a file in the project."""
119
  try:
120
- full_path = os.path.join(project_path, file_path)
121
  with open(full_path, "r") as f:
122
  content = f.read()
123
  return content
124
  except Exception as e:
125
  return f"Error reading file: {str(e)}"
126
-
127
- def write_file(file_path: str, content: str, project_path: str = DEFAULT_PROJECT_PATH) -> str:
128
  """Writes content to a file in the project."""
129
  try:
130
- full_path = os.path.join(project_path, file_path)
131
- with open(full_path, "w") as f:
132
- f.write(content)
133
- return f"Successfully wrote to '{file_path}'"
134
  except Exception as e:
135
  return f"Error writing to file: {str(e)}"
136
 
@@ -147,54 +138,47 @@ def preview(project_path: str = DEFAULT_PROJECT_PATH):
147
  else:
148
  return "No 'index.html' found for preview."
149
  except Exception as e:
150
- return f"Error previewing project: {str(e)}"
151
 
152
  def main():
153
- with gr.Blocks() as demo:
154
- gr.Markdown("## IDEvIII: Your Hugging Face No-Code App Builder")
155
-
156
- # --- Model Selection ---
157
- with gr.Tab("Model"):
158
- # --- Model Dropdown with Categories ---
159
  model_categories = gr.Dropdown(
160
- choices=["Text Generation", "Text Summarization", "Code Generation", "Translation", "Question Answering"],
161
  label="Model Category",
162
- value="Text Generation"
163
- )
164
- model_name = gr.Dropdown(
165
- choices=[], # Initially empty, will be populated based on category
166
- label="Hugging Face Model Name",
167
  )
168
  load_button = gr.Button("Load Model")
169
  load_output = gr.Textbox(label="Output")
170
  model_description = gr.Markdown(label="Model Description")
171
 
172
- # --- Function to populate model names based on category ---
173
- def update_model_dropdown(category):
174
  models = []
175
  api = HfApi()
176
  for model in api.list_models():
177
- if model.pipeline_tag == category:
178
- models.append(model.modelId)
179
- return gr.Dropdown.update(choices=models)
180
 
181
  # --- Event handler for category dropdown ---
182
  model_categories.change(
183
- fn=update_model_dropdown,
184
- inputs=model_categories,
185
  outputs=model_name,
186
  )
187
-
188
  # --- Event handler to display model description ---
189
  def display_model_description(model_name):
190
  global model_descriptions
191
  if model_name in model_descriptions:
192
- return model_descriptions[model_name]
193
  else:
194
- return "Model description not available."
195
 
196
  model_name.change(
197
- fn=display_model_description,
198
  inputs=model_name,
199
  outputs=model_description,
200
  )
@@ -211,126 +195,114 @@ def main():
211
  load_button.click(load_selected_model, inputs=model_name, outputs=load_output)
212
 
213
  # --- Chat Interface ---
214
- with gr.Tab("Chat"):
215
- chatbot = gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True)
216
- message = gr.Textbox(label="Enter your message", placeholder="Ask me anything!")
217
- purpose = gr.Textbox(label="Purpose", placeholder="What is the purpose of this interaction?")
218
- agent_name = gr.Dropdown(label="Agents", choices=["Generic Agent"], value="Generic Agent", interactive=True)
219
- sys_prompt = gr.Textbox(label="System Prompt", max_lines=1, interactive=True)
220
- temperature = gr.Slider(label="Temperature", value=TEMPERATURE, minimum=0.0, maximum=1.0, step=0.05, interactive=True, info="Higher values produce more diverse outputs")
221
- max_new_tokens = gr.Slider(label="Max new tokens", value=MAX_TOKENS, minimum=0, maximum=1048 * 10, step=64, interactive=True, info="The maximum numbers of new tokens")
222
- top_p = gr.Slider(label="Top-p (nucleus sampling)", value=TOP_P, minimum=0.0, maximum=1, step=0.05, interactive=True, info="Higher values sample more low-probability tokens")
223
- repetition_penalty = gr.Slider(label="Repetition penalty", value=REPETITION_PENALTY, minimum=1.0, maximum=2.0, step=0.05, interactive=True, info="Penalize repeated tokens")
224
  submit_button = gr.Button(value="Send")
225
  history = gr.State([])
226
 
227
- def run_chat(purpose: str, message: str, agent_name: str, sys_prompt: str, temperature: float, max_new_tokens: int, top_p: float, repetition_penalty: float, history: List[Tuple[str, str]]) -> Tuple[List[Tuple[str, str]], List[Tuple[str, str]]]:
228
  if not current_model:
229
  return [(history, history), "Please load a model first."]
230
-
231
- def generate_response(message, history, agent_name, sys_prompt, temperature, max_new_tokens, top_p, repetition_penalty):
232
- if not current_model:
233
- return "Please load a model first."
234
-
235
- conversation = [{"role": "system", "content": sys_prompt}]
236
- for message, response in history:
237
- conversation.append({"role": "user", "content": message})
238
- conversation.append({"role": "assistant", "content": response})
239
- conversation.append({"role": "user", "content": message})
240
-
241
- response = current_model.generate(
242
- conversation,
243
- max_new_tokens=max_new_tokens,
244
- temperature=temperature,
245
- top_p=top_p,
246
- repetition_penalty=repetition_penalty,
247
- )
248
-
249
- return response.text.strip()
250
-
251
- def create_project(project_name):
252
- try:
253
- repo_name = get_full_repo_name(project_name, token=HfApi().token)
254
- repo = HfFolder.create_repo(repo_name, exist_ok=True)
255
- repo.save_data("README.md", f"# {project_name}")
256
- return f"Created project '{project_name}' on Hugging Face Hub."
257
- except Exception as e:
258
- return f"Error creating project: {str(e)}"
259
-
260
- def read_file(file_path):
261
- if not os.path.exists(file_path):
262
- return f"File '{file_path}' does not exist."
263
-
264
- try:
265
- with open(file_path, "r") as file:
266
- content = file.read()
267
- return content
268
- except Exception as e:
269
- return f"Error reading file '{file_path}': {str(e)}"
270
-
271
- def write_file(file_path, file_content):
272
- try:
273
- with open(file_path, "w") as file:
274
- file.write(file_content)
275
- return f"Wrote to file '{file_path}' successfully."
276
- except Exception as e:
277
- return f"Error writing to file '{file_path}': {str(e)}"
278
-
279
- def run_command(command):
280
- try:
281
- result = subprocess.run(command, shell=True, capture_output=True, text=True)
282
- if result.returncode == 0:
283
- return result.stdout
284
- else:
285
- return f"Command '{command}' failed with exit code {result.returncode}:\n{result.stderr}"
286
- except Exception as e:
287
- return f"Error running command '{command}': {str(e)}"
288
-
289
-
290
- def preview():
291
- # Get the current working directory
292
- cwd = os.getcwd()
293
-
294
- # Create a temporary directory for the preview
295
- temp_dir = tempfile.mkdtemp()
296
-
297
- try:
298
- # Copy the project files to the temporary directory
299
- shutil.copytree(cwd, temp_dir, ignore=shutil.ignore_patterns("__pycache__", "*.pyc"))
300
-
301
- # Change to the temporary directory
302
- os.chdir(temp_dir)
303
-
304
- # Find the main Python file (e.g., app.py, main.py)
305
- main_file = next((f for f in os.listdir(".") if f.endswith(".py")), None)
306
 
307
- if main_file:
308
- # Run the main Python file to generate the preview
309
- subprocess.run(["streamlit", "run", main_file], check=True)
310
-
311
- # Get the preview URL
312
- preview_url = components.get_url(main_file)
313
-
314
- # Change back to the original working directory
315
- os.chdir(cwd)
316
-
317
- # Return the preview URL
318
- return preview_url
319
- else:
320
- return "No main Python file found in the project."
321
- except Exception as e:
322
- return f"Error generating preview: {str(e)}"
323
- finally:
324
- # Remove the temporary directory
325
- shutil.rmtree(temp_dir)
326
-
327
- # Customize the launch settings
328
- server_name = "0.0.0.0" # Listen on all available network interfaces
329
- server_port = 7860 # Choose an available port
330
- share_gradio_link = True # Share a public URL for the app
331
-
332
- # Launch the interface
333
- demo.launch(server_name=server_name, server_port=server_port, share=share_gradio_link)
334
-
335
- if __name__ == "__main__":
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
336
  main()
 
8
 
9
  import gradio as gr
10
  from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
11
+ huggingface_hub import InferenceClient, cached_download, Repository, HfApi
12
  from IPython.display import display, HTML
13
  import streamlit.components.v1 as components
14
 
 
48
 
49
  # Fetch and store the model description
50
  api = HfApi()
51
+ model_info =.model_info(model_name)
52
  model_descriptions[model_name] = model_info.pipeline_tag
53
  return f"Successfully loaded model: {model_name}"
54
  except Exception as e:
 
59
  st.write("Select a model to use for code generation:")
60
  models = ["distilbert", "t5", "codellama-7b", "geminai-1.5b"]
61
  selected_model = st.selectbox("Select a model:", models)
62
+ if selected_:
63
  model = load_model(selected_model)
64
  if model:
65
  st.write(f"Model {selected_model} imported successfully!")
 
72
  """Executes a shell command and returns the output."""
73
  try:
74
  if project_path:
75
+ process = subprocess.Popen(command, shell=True, cwdproject_path, stdout=subprocess.PIPE, stderr=subprocess.PIPE) else:
 
76
  process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
77
  output, error = process.communicate()
78
  if error:
79
  return f"Error: {error.decode('utf-8')}"
80
+ return.decode("utf-8
81
  except Exception as e:
82
+ return f"Error executing command: {stre)}"
83
+ _project(project_name: str, project_path: str = DEFAULT_PROJECTPATH):
 
84
  """Creates a new Hugging Face project."""
85
  global repo
86
+ try os.path.exists(project_path):
 
87
  return f"Error: Directory '{project_path}' already exists!"
88
  # Create the repository
89
  repo = Repository(local_dir=project_path, clone_from=None)
90
  repo.git_init()
91
 
92
+ # Add basic filesoptional, can customize this) with open(path.join(_path, "README.md"), "w") as f: f.write(f {project_name}\n\nA new Face project.")
 
 
93
 
94
+ # Stage all changes repo.git_add(pattern="*")
 
95
  repo.git_commit(commit_message="Initial commit")
96
 
97
+ return f"Hugging Face project '{project_name}' created successfully at '{project_"
98
  except Exception as e:
99
  return f"Error creating Hugging Face project: {str(e)}"
100
 
101
+ def list(project_path: str = DEFAULT_PROJECT_PATH) -> str:
102
  """Lists files in the project directory."""
103
  try:
104
  files = os.listdir(project_path)
105
  if not files:
106
  return "Project directory is empty."
107
  return "\n".join(files)
108
+ except Exception as e: return f"Error listing project {str()}"
 
109
 
110
+ def read_file(filepath: str, project_path: str = DEFAULT_PROPATH) -> str """Reads and returns the content of a file in the project."""
 
111
  try:
112
+ _path = os.path.join(project_path, file_path)
113
  with open(full_path, "r") as f:
114
  content = f.read()
115
  return content
116
  except Exception as e:
117
  return f"Error reading file: {str(e)}"
118
+ def write_file(file_: str, content str project_path str =PROJECT_PATH:
 
119
  """Writes content to a file in the project."""
120
  try:
121
+ full_path = os.path.join(project, file_path)
122
+ with open(full_path, "") as f:
123
+ f.(
124
+ return"Successfully wrote to '{_path}'"
125
  except Exception as e:
126
  return f"Error writing to file: {str(e)}"
127
 
 
138
  else:
139
  return "No 'index.html' found for preview."
140
  except Exception as e:
141
+ return f preview project: {str(e)}"
142
 
143
  def main():
144
+ .Blocks() as demo:
145
+ gr.Markdown("## IDEvIII: Your Hugging No- App Builder")
146
+ --- Model Selection --- with gr.Tab("Model"): --- Model Drop with Categories ---
 
 
 
147
  model_categories = gr.Dropdown(
148
+ choices=Text Generation", "Text Summarization", "Code Generation", "Translation", "Question Answering"],
149
  label="Model Category",
150
+ value=" Generation" )
151
+ _name = gr.Dropdown(
152
+ choices=[], # Initially empty, will be pop based on category
153
+ label="Hugging Face Model Name",
 
154
  )
155
  load_button = gr.Button("Load Model")
156
  load_output = gr.Textbox(label="Output")
157
  model_description = gr.Markdown(label="Model Description")
158
 
159
+ # --- Function to pop model names category ---
160
+ update_modeldropdown(category):
161
  models = []
162
  api = HfApi()
163
  for model in api.list_models():
164
+ if model.pipeline_tag ==
165
+ models.append(model.modelId) return gr.Dropdown.update(choices=models)
 
166
 
167
  # --- Event handler for category dropdown ---
168
  model_categories.change(
169
+ fn=update_model_ inputs=model_categories,
 
170
  outputs=model_name,
171
  )
 
172
  # --- Event handler to display model description ---
173
  def display_model_description(model_name):
174
  global model_descriptions
175
  if model_name in model_descriptions:
176
+ return model_descriptions[modelname]
177
  else:
178
+ return "Model description available."
179
 
180
  model_name.change(
181
+ =display_model_description,
182
  inputs=model_name,
183
  outputs=model_description,
184
  )
 
195
  load_button.click(load_selected_model, inputs=model_name, outputs=load_output)
196
 
197
  # --- Chat Interface ---
198
+ with gr.Tab("Chat
199
+ chatbot gr.Chatbot(show_label=False, show_share_button=False_copy_button, likeable)
200
+ message = gr.Textbox(Enter your message="Ask me anything!")
201
+ purpose = gr.Textbox(label="Purpose", placeholder="What is the of this interaction)
202
+ agent_name = gr.(label="Ag=["Generic Agent"], value="Generic Agent", interactive=True)
203
+ prompt = gr.Textboxlabel="System Prompt", max_lines=1, interactive=True)
204
+ temperature = gr.Slider(label="Temperature", value=TEMPERATURE, minimum=0.0, maximum=1.0, step=0.05, interactive=True, info="Higher values produce more max_newtokens =Slider(labelMax new tokens", value=MAX_TOKENS, minimum=0, maximum=1048 * 10, step=64, interactive=True, info="The maximum numbers of new tokens")
205
+ top_p = gr.Slider(label="Top-p (nucleus sampling)", valueTOP_P, minimum=0, maximum=1 step=0.05, interactive=True, info="Higher values sample more low-probability tokens")
206
+ repetition_penalty = gr.Slider(label="Repetition penalty", value=REPETITION_PENALTY minimum=1., maximum=2.0,=0.05, interactive=True, info="Penalize repeated tokens")
 
207
  submit_button = gr.Button(value="Send")
208
  history = gr.State([])
209
 
210
+ run_chat(purpose: str, message: str, agent_name str, sys_prompt: str, temperature: float, max_new_tokens: int, top_p: float, repetition_penalty: float, history: List[Tuple[str, str]]) -> Tuple[List[Tuple[str,]], List[[str, str]]]:
211
  if not current_model:
212
  return [(history, history), "Please load a model first."]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
213
 
214
+ def generate_response(message, history, agent_name, sys_prompt, temperature, max_new_tokens, top, repetition_penalty):
215
+ if not current_model:
216
+ return "Please load a model first."
217
+
218
+ conversation = [{"role": "system", "content sys_pt}]
219
+ for message, response history:
220
+ conversationappend({": "", "content": message})
221
+ conversation.append({"": "assistant", "content": response})
222
+ conversation.append({"role": "user", "content": message})
223
+
224
+ response = currentmodel.generate(
225
+ conversation,
226
+ max_new_tokensmax_new_tokens,
227
+ temperaturetemperature,
228
+ top_p=top_p,
229
+ repetition_penalty=petition_al
230
+ )
231
+
232
+ response.text.strip()
233
+
234
+ def create_project(project_name):
235
+ try:
236
+ repo_name = get_full_repo_name(project_name, token=HfApi().token)
237
+ repofFolder.create_repo(repo_name, exist_ok=True)
238
+ repo.save_data("README.md", f"# {project_name
239
+ return f" '{project_name}' on Hugging Face Hub."
240
+ except Exception as e:
241
+ return"Error project: {str(e)}
242
+ def read_file(file_path):
243
+ if not os.path.exists(file_path):
244
+ return f"File_path}' does exist."
245
+
246
+ try
247
+ with open(file, "r") as file: content = file()
248
+ return content
249
+ as e:
250
+ return f"Error reading file '{file_path}': {str(e)}"
251
+
252
+ def write_file(file_path, file_content): try
253
+ with open(file_ "w") as file:
254
+ file.write(_content)
255
+ f"Wrote to file '{file_path}' successfully."
256
+ except Exception as e:
257
+ return f"Error writing to file '{file_path}': {str(e)}"
258
+
259
+ def run_command(command):
260
+ try:
261
+ result =.run(command shell=True, capture_outputTrue,=True)
262
+ if result.returncode == 0:
263
+ return result.stdout else:
264
+ return f"Command '{command failed with exit code {.}:\n{result.stderr}"
265
+ except Exception:
266
+ return f"Error running command '{command}': {str(e)}"
267
+
268
+ def preview():
269
+ # Get the current working directory
270
+ cwd = os.getcwd()
271
+
272
+ # Create a temporary directory for the preview
273
+ temp_dir = tempfile.mkdtemp()
274
+
275
+ try:
276
+ Copy the project files the temporary directory
277
+ shutil.copytree(cwd, temp_dir, ignore=shutil.ignore_patterns("__py__", "*.pyc"))
278
+ # Change to the temporary directory
279
+ os.chdir(temp_dir)
280
+ # Find the Python file (e.g., app.py, main.py)
281
+ main_file = next((f for f in os.listdir(".") if f.endswith(".py")), None)
282
+
283
+ if main_file:
284
+ # Run the main Python file to generate the preview
285
+ subprocess.run(["streamlit", "run", main_file], check)
286
+
287
+ # Get preview URL
288
+ preview_url = components.get_url(_file)
289
+
290
+ # Change back to original working directory
291
+ os.chdir(cwd)
292
+ # Return the preview URL return preview_url
293
+ else:
294
+ return "No main found in the project."
295
+ except Exception as:
296
+ return f"Error generating preview: {str(e)}" finally:
297
+ # Remove the directory
298
+ .rmtree(tempdir)
299
+
300
+ # Custom server_ = "0.0.0. # Listen on available network interfaces
301
+ server_port 760 # an available
302
+ sharegradio_link = True # Share a public URL for the app
303
+
304
+ # Launch the interface
305
+ demo.launch(server_name=server, server_portserver_port, shareshare_gradio)
306
+
307
+ if __name "__main__":
308
  main()