Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,311 +1,99 @@
|
|
1 |
import os
|
2 |
-
import
|
3 |
-
import random
|
4 |
import time
|
5 |
-
from typing import Dict, List, Tuple
|
6 |
-
from datetime import datetime
|
7 |
-
import logging
|
8 |
-
import huggingface_hub as hfApi
|
9 |
-
import gradio as gr
|
10 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
11 |
-
from huggingface_hub import InferenceClient, cached_download, Repository
|
12 |
-
from IPython.display import display, HTML
|
13 |
-
import streamlit.components.v1 as components
|
14 |
-
import tempfile
|
15 |
-
import shutil
|
16 |
|
17 |
-
|
18 |
-
VERBOSE = True
|
19 |
-
MAX_HISTORY = 5
|
20 |
-
MAX_TOKENS = 2048
|
21 |
-
TEMPERATURE = 0.7
|
22 |
-
TOP_P = 0.8
|
23 |
-
REPETITION_PENALTY = 1.5
|
24 |
-
DEFAULT_PROJECT_PATH = "./my-hf-project" # Default project directory
|
25 |
|
26 |
-
|
27 |
-
logging.basicConfig(
|
28 |
-
filename="app.log",
|
29 |
-
level=logging.INFO,
|
30 |
-
format="%(asctime)s - %(levelname)s - %(message)s",
|
31 |
-
)
|
32 |
|
33 |
-
|
34 |
-
current_model = None # Store the currently loaded model
|
35 |
-
repo = None # Store the Hugging Face Repository object
|
36 |
-
model_descriptions = {} # Store model descriptions
|
37 |
|
38 |
-
|
39 |
-
def load_model(model_name: str):
|
40 |
-
"""Loads a language model and fetches its description."""
|
41 |
-
global current_model, model_descriptions
|
42 |
-
try:
|
43 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
44 |
-
current_model = pipeline(
|
45 |
-
"text-generation",
|
46 |
-
model=model_name,
|
47 |
-
tokenizer=tokenizer,
|
48 |
-
model_kwargs={"load_in_8bit": True}
|
49 |
-
)
|
50 |
|
51 |
-
|
52 |
-
api = HfApi()
|
53 |
-
model_info = api.model_info(model_name)
|
54 |
-
model_descriptions[model_name] = model_info.pipeline_tag
|
55 |
-
return f"Successfully loaded model: {model_name}"
|
56 |
-
except Exception as e:
|
57 |
-
return f"Error loading model: {str(e)}"
|
58 |
|
59 |
-
|
60 |
-
st.title("Model Selection")
|
61 |
-
st.write("Select a model to use for code generation:")
|
62 |
-
models = ["distilbert", "t5", "codellama-7b", "geminai-1.5b"]
|
63 |
-
selected_model = st.selectbox("Select a model:", models)
|
64 |
-
if selected_model:
|
65 |
-
model = load_model(selected_model)
|
66 |
-
if model:
|
67 |
-
st.write(f"Model {selected_model} imported successfully!")
|
68 |
-
return model
|
69 |
-
else:
|
70 |
-
st.write(f"Error importing model {selected_model}.")
|
71 |
-
return None
|
72 |
|
73 |
-
|
74 |
-
"""Executes a shell command and returns the output."""
|
75 |
-
try:
|
76 |
-
if project_path:
|
77 |
-
process = subprocess.Popen(command, shell=True, cwd=project_path, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
78 |
-
else:
|
79 |
-
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
80 |
-
output, error = process.communicate()
|
81 |
-
if error:
|
82 |
-
return f"Error: {error.decode('utf-8')}"
|
83 |
-
return output.decode('utf-8')
|
84 |
-
except Exception as e:
|
85 |
-
return f"Error executing command: {str(e)}"
|
86 |
|
87 |
-
def
|
88 |
-
|
89 |
-
|
90 |
-
try:
|
91 |
-
if os.path.exists(project_path):
|
92 |
-
return f"Error: Directory '{project_path}' already exists!"
|
93 |
-
# Create the repository
|
94 |
-
repo = Repository(local_dir=project_path, clone_from=None)
|
95 |
-
repo.git_init()
|
96 |
-
# Add basic files (optional, can customize this)
|
97 |
-
with open(os.path.join(project_path, "README.md"), "w") as f:
|
98 |
-
f.write(f"# {project_name}\n\nA new Hugging Face project.")
|
99 |
-
# Stage all changes
|
100 |
-
repo.git_add(pattern="*")
|
101 |
-
repo.git_commit(commit_message="Initial commit")
|
102 |
-
return f"Hugging Face project '{project_name}' created successfully at '{project_path}'"
|
103 |
-
except Exception as e:
|
104 |
-
return f"Error creating Hugging Face project: {str(e)}"
|
105 |
|
106 |
-
|
107 |
-
|
108 |
-
try:
|
109 |
-
files = os.listdir(project_path)
|
110 |
-
if not files:
|
111 |
-
return "Project directory is empty."
|
112 |
-
return "\n".join(files)
|
113 |
-
except Exception as e:
|
114 |
-
return f"Error listing project files: {str(e)}"
|
115 |
|
116 |
-
|
117 |
-
"""Reads and returns the content of a file in the project."""
|
118 |
try:
|
119 |
-
|
120 |
-
with open(full_path, "r") as f:
|
121 |
-
content = f.read()
|
122 |
-
return content
|
123 |
except Exception as e:
|
124 |
-
|
|
|
125 |
|
126 |
-
|
127 |
-
"""Writes content to a file in the project."""
|
128 |
try:
|
129 |
-
|
130 |
-
with open(full_path, "w") as f:
|
131 |
-
f.write(content)
|
132 |
-
return f"Successfully wrote to '{full_path}'"
|
133 |
except Exception as e:
|
134 |
-
|
|
|
135 |
|
136 |
-
|
137 |
-
"""Provides a preview of the project, if applicable."""
|
138 |
-
# Assuming a simple HTML preview for now
|
139 |
try:
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
display(HTML(html_content))
|
145 |
-
return "Previewing 'index.html'"
|
146 |
-
else:
|
147 |
-
return "No 'index.html' found for preview."
|
148 |
except Exception as e:
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
)
|
199 |
-
|
200 |
-
# --- Event handler to load the selected model ---
|
201 |
-
def load_selected_model(model_name):
|
202 |
-
global current_model
|
203 |
-
load_output = load_model(model_name)
|
204 |
-
if current_model:
|
205 |
-
return f"Model '{model_name}' loaded successfully!"
|
206 |
-
else:
|
207 |
-
return f"Error loading model '{model_name}'"
|
208 |
-
load_button.click(load_selected_model, inputs=model_name, outputs=load_output)
|
209 |
-
|
210 |
-
# --- Chat Interface ---
|
211 |
-
with gr.Tab("Chat"):
|
212 |
-
chatbot = gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True)
|
213 |
-
message = gr.Textbox(label="Enter your message", placeholder="Ask me anything!")
|
214 |
-
purpose = gr.Textbox(label="Purpose", placeholder="What is the purpose of this interaction?")
|
215 |
-
agent_name = gr.Textbox(label="Agent Name", value="Generic Agent", interactive=True)
|
216 |
-
sys_prompt = gr.Textbox(label="System Prompt", max_lines=1, interactive=True)
|
217 |
-
temperature = gr.Slider(label="Temperature", value=TEMPERATURE, minimum=0.0, maximum=1.0, step=0.05, interactive=True, info="Higher values produce more random results")
|
218 |
-
max_new_tokens = gr.Slider(label="Max new tokens", value=MAX_TOKENS, minimum=0, maximum=1048 * 10, step=64, interactive=True, info="The maximum number of new tokens")
|
219 |
-
top_p = gr.Slider(label="Top-p (nucleus sampling)", value=TOP_P, minimum=0, maximum=1, step=0.05, interactive=True, info="Higher values sample more low-probability tokens")
|
220 |
-
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")
|
221 |
-
submit_button = gr.Button(value="Send")
|
222 |
-
history = gr.State([])
|
223 |
-
|
224 |
-
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]]]:
|
225 |
-
if not current_model:
|
226 |
-
return [(history, history), "Please load a model first."]
|
227 |
-
def generate_response(message, history, agent_name, sys_prompt, temperature, max_new_tokens, top_p, repetition_penalty):
|
228 |
-
if not current_model:
|
229 |
-
return "Please load a model first."
|
230 |
-
conversation = [{"role": "system", "content": sys_prompt}]
|
231 |
-
for message, response in history:
|
232 |
-
conversation.append({"role": "user", "content": message})
|
233 |
-
conversation.append({"role": "assistant", "content": response})
|
234 |
-
conversation.append({"role": "user", "content": message})
|
235 |
-
response = current_model.generate(
|
236 |
-
conversation,
|
237 |
-
max_new_tokens=max_new_tokens,
|
238 |
-
temperature=temperature,
|
239 |
-
top_p=top_p,
|
240 |
-
repetition_penalty=repetition_penalty
|
241 |
-
)
|
242 |
-
return response.text.strip()
|
243 |
-
response_text = generate_response(message, history, agent_name, sys_prompt, temperature, max_new_tokens, top_p, repetition_penalty)
|
244 |
-
history.append((message, response_text))
|
245 |
-
return history, history
|
246 |
-
|
247 |
-
submit_button.click(run_chat, inputs=[purpose, message, agent_name, sys_prompt, temperature, max_new_tokens, top_p, repetition_penalty, history], outputs=[chatbot, history])
|
248 |
-
|
249 |
-
# --- Project Management ---
|
250 |
-
with gr.Tab("Project Management"):
|
251 |
-
project_name_input = gr.Textbox(label="Project Name", placeholder="Enter project name")
|
252 |
-
create_project_button = gr.Button("Create Project")
|
253 |
-
project_output = gr.Textbox(label="Output")
|
254 |
-
|
255 |
-
def create_project_action(project_name):
|
256 |
-
return create_project(project_name)
|
257 |
-
|
258 |
-
create_project_button.click(create_project_action, inputs=project_name_input, outputs=project_output)
|
259 |
-
|
260 |
-
list_files_button = gr.Button("List Files")
|
261 |
-
list_files_output = gr.Textbox(label="Files")
|
262 |
-
|
263 |
-
def list_files_action():
|
264 |
-
return list_files()
|
265 |
-
|
266 |
-
list_files_button.click(list_files_action, outputs=list_files_output)
|
267 |
-
|
268 |
-
file_path_input = gr.Textbox(label="File Path", placeholder="Enter file path")
|
269 |
-
read_file_button = gr.Button("Read File")
|
270 |
-
read_file_output = gr.Textbox(label="File Content")
|
271 |
-
|
272 |
-
def read_file_action(file_path):
|
273 |
-
return read_file(file_path)
|
274 |
-
|
275 |
-
read_file_button.click(read_file_action, inputs=file_path_input, outputs=read_file_output)
|
276 |
-
|
277 |
-
write_file_button = gr.Button("Write File")
|
278 |
-
file_content_input = gr.Textbox(label="File Content", placeholder="Enter file content")
|
279 |
-
|
280 |
-
def write_file_action(file_path, file_content):
|
281 |
-
return write_file(file_path, file_content)
|
282 |
-
|
283 |
-
write_file_button.click(write_file_action, inputs=[file_path_input, file_content_input], outputs=project_output)
|
284 |
-
|
285 |
-
run_command_input = gr.Textbox(label="Command", placeholder="Enter command")
|
286 |
-
run_command_button = gr.Button("Run Command")
|
287 |
-
run_command_output = gr.Textbox(label="Command Output")
|
288 |
-
|
289 |
-
def run_command_action(command):
|
290 |
-
return run_command(command)
|
291 |
-
|
292 |
-
run_command_button.click(run_command_action, inputs=run_command_input, outputs=run_command_output)
|
293 |
-
|
294 |
-
preview_button = gr.Button("Preview Project")
|
295 |
-
preview_output = gr.Textbox(label="Preview URL")
|
296 |
-
|
297 |
-
def preview_action():
|
298 |
-
return preview()
|
299 |
-
|
300 |
-
preview_button.click(preview_action, outputs=preview_output)
|
301 |
-
|
302 |
-
# Custom server settings
|
303 |
-
server_name = "0.0.0.0" # Listen on all available network interfaces
|
304 |
-
server_port = 7860# Choose an available port
|
305 |
-
share_gradio_link = True # Share a public URL for the app
|
306 |
-
|
307 |
-
# Launch the interface
|
308 |
-
demo.launch(server_name=server_name, server_port=server_port, share=share_gradio_link)
|
309 |
|
310 |
if __name__ == "__main__":
|
311 |
main()
|
|
|
1 |
import os
|
2 |
+
import sys
|
|
|
3 |
import time
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
+
import huggingface_hub
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
+
from huggingface_hub.commands import HfFolder
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
+
import transformers
|
|
|
|
|
|
|
10 |
|
11 |
+
from transformers import pipeline
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
+
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
import tempfile
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
+
from huggingface_hub import HfFolder
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
+
def main():
|
20 |
+
# Get the user's idea
|
21 |
+
idea = input("What is your idea for an application? ")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
+
# Generate the code for the application
|
24 |
+
code = gemmacode.generate(idea)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
+
# Test the code
|
|
|
27 |
try:
|
28 |
+
transformers.pipeline("text-generation")(code)
|
|
|
|
|
|
|
29 |
except Exception as e:
|
30 |
+
print("The code failed to run:", e)
|
31 |
+
return
|
32 |
|
33 |
+
# Ensure the functionality of the application
|
|
|
34 |
try:
|
35 |
+
gr.Interface(fn=transformers.pipeline("text-generation"), inputs=gr.Textbox(), outputs=gr.Textbox()).launch()
|
|
|
|
|
|
|
36 |
except Exception as e:
|
37 |
+
print("The application failed to run:", e)
|
38 |
+
return
|
39 |
|
40 |
+
# Provide an embedded webapp demo of the user's idea implementation
|
|
|
|
|
41 |
try:
|
42 |
+
hf_folder = HfFolder(path=tempfile.mkdtemp())
|
43 |
+
hf_folder.save(code)
|
44 |
+
hf_folder.push_to_hub(repo_id="acecalisto3/gemmacode-demo", commit_message="Initial commit")
|
45 |
+
print(f"The demo is available at: https://huggingface.co/acecalisto3/gemmacode-demo")
|
|
|
|
|
|
|
|
|
46 |
except Exception as e:
|
47 |
+
print("The demo failed to launch:", e)
|
48 |
+
return
|
49 |
+
|
50 |
+
# Offer the option to rebuild or deploy
|
51 |
+
while True:
|
52 |
+
choice = input("Do you want to rebuild or deploy the application? (r/d/q) ")
|
53 |
+
if choice == "r":
|
54 |
+
# Rebuild the code
|
55 |
+
code = gemmacode.generate(idea)
|
56 |
+
|
57 |
+
# Test the code
|
58 |
+
try:
|
59 |
+
transformers.pipeline("text-generation")(code)
|
60 |
+
except Exception as e:
|
61 |
+
print("The code failed to run:", e)
|
62 |
+
return
|
63 |
+
|
64 |
+
# Ensure the functionality of the application
|
65 |
+
try:
|
66 |
+
gr.Interface(fn=transformers.pipeline("text-generation"), inputs=gr.Textbox(), outputs=gr.Textbox()).launch()
|
67 |
+
except Exception as e:
|
68 |
+
print("The application failed to run:", e)
|
69 |
+
return
|
70 |
+
|
71 |
+
# Provide an embedded webapp demo of the user's idea implementation
|
72 |
+
try:
|
73 |
+
hf_folder = HfFolder(path=tempfile.mkdtemp())
|
74 |
+
hf_folder.save(code)
|
75 |
+
hf_folder.push_to_hub(repo_id="acecalisto3/gemmacode-demo", commit_message="Initial commit")
|
76 |
+
print(f"The demo is available at: https://huggingface.co/acecalisto3/gemmacode-demo")
|
77 |
+
except Exception as e:
|
78 |
+
print("The demo failed to launch:", e)
|
79 |
+
return
|
80 |
+
elif choice == "d":
|
81 |
+
# Deploy the application
|
82 |
+
try:
|
83 |
+
api_token = os.environ["HF_TOKEN"]
|
84 |
+
hub = huggingface_hub.HfApi(api_token=api_token)
|
85 |
+
hub.create_repo(name="my-app", organization="my-org")
|
86 |
+
hf_folder = HfFolder(path=tempfile.mkdtemp())
|
87 |
+
hf_folder.save(code)
|
88 |
+
hf_folder.push_to_hub(repo_id="my-org/my-app", commit_message="Initial commit")
|
89 |
+
print("The application has been deployed to: https://huggingface.co/my-org/my-app")
|
90 |
+
except Exception as e:
|
91 |
+
print("The application failed to deploy:", e)
|
92 |
+
return
|
93 |
+
elif choice == "q":
|
94 |
+
break
|
95 |
+
else:
|
96 |
+
print("Invalid choice")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
|
98 |
if __name__ == "__main__":
|
99 |
main()
|