Spaces:
Running
Running
acecalisto3
commited on
Commit
•
a3f74af
1
Parent(s):
2676e9c
Update app.py
Browse files
app.py
CHANGED
@@ -3,7 +3,9 @@ import os
|
|
3 |
import subprocess
|
4 |
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
|
5 |
import black
|
6 |
-
import
|
|
|
|
|
7 |
|
8 |
# Define functions for each feature
|
9 |
|
@@ -18,13 +20,13 @@ def chat_interface(input_text):
|
|
18 |
The chatbot's response.
|
19 |
"""
|
20 |
# Load the GPT-2 model which is compatible with AutoModelForCausalLM
|
21 |
-
model_name =
|
22 |
try:
|
23 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
24 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
25 |
-
generator = pipeline(
|
26 |
except EnvironmentError as e:
|
27 |
-
return f
|
28 |
|
29 |
# Truncate input text to avoid exceeding the model's maximum length
|
30 |
max_input_length = 900
|
@@ -33,16 +35,20 @@ def chat_interface(input_text):
|
|
33 |
input_ids = input_ids[:, :max_input_length]
|
34 |
|
35 |
# Generate chatbot response
|
36 |
-
outputs = model.generate(
|
|
|
|
|
37 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
38 |
return response
|
39 |
|
|
|
40 |
# 2. Terminal
|
41 |
-
def terminal_interface(command):
|
42 |
"""Executes commands in the terminal.
|
43 |
|
44 |
Args:
|
45 |
command: User's command.
|
|
|
46 |
|
47 |
Returns:
|
48 |
The terminal output.
|
@@ -51,10 +57,18 @@ def terminal_interface(command):
|
|
51 |
try:
|
52 |
process = subprocess.run(command.split(), capture_output=True, text=True)
|
53 |
output = process.stdout
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
except Exception as e:
|
55 |
-
output = f
|
56 |
return output
|
57 |
|
|
|
58 |
# 3. Code Editor
|
59 |
def code_editor_interface(code):
|
60 |
"""Provides code completion, formatting, and linting in the code editor.
|
@@ -73,14 +87,14 @@ def code_editor_interface(code):
|
|
73 |
|
74 |
# Lint code using pylint
|
75 |
try:
|
76 |
-
|
77 |
-
|
78 |
-
lint_message = f"Pylint score: {lint_results:.2f}"
|
79 |
except Exception as e:
|
80 |
lint_message = f"Pylint error: {e}"
|
81 |
|
82 |
return formatted_code, lint_message
|
83 |
|
|
|
84 |
# 4. Workspace
|
85 |
def workspace_interface(project_name):
|
86 |
"""Manages projects, files, and resources in the workspace.
|
@@ -91,18 +105,46 @@ def workspace_interface(project_name):
|
|
91 |
Returns:
|
92 |
Project creation status.
|
93 |
"""
|
|
|
94 |
# Create project directory
|
95 |
try:
|
96 |
-
os.makedirs(
|
97 |
-
|
|
|
|
|
|
|
98 |
except FileExistsError:
|
99 |
-
status = f'Project
|
100 |
return status
|
101 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
102 |
# 5. AI-Infused Tools
|
103 |
|
104 |
# Define custom AI-powered tools using Hugging Face models
|
105 |
|
|
|
106 |
# Example: Text summarization tool
|
107 |
def summarize_text(text):
|
108 |
"""Summarizes a given text using a Hugging Face model.
|
@@ -114,11 +156,11 @@ def summarize_text(text):
|
|
114 |
Summarized text.
|
115 |
"""
|
116 |
# Load the summarization model
|
117 |
-
model_name =
|
118 |
try:
|
119 |
-
summarizer = pipeline(
|
120 |
except EnvironmentError as e:
|
121 |
-
return f
|
122 |
|
123 |
# Truncate input text to avoid exceeding the model's maximum length
|
124 |
max_input_length = 1024
|
@@ -127,9 +169,55 @@ def summarize_text(text):
|
|
127 |
inputs = text[:max_input_length]
|
128 |
|
129 |
# Generate summary
|
130 |
-
summary = summarizer(inputs, max_length=100, min_length=30, do_sample=False)[0][
|
|
|
|
|
131 |
return summary
|
132 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
133 |
# 6. Code Generation
|
134 |
def generate_code(idea):
|
135 |
"""Generates code based on a given idea using the EleutherAI/gpt-neo-2.7B model.
|
@@ -142,12 +230,12 @@ def generate_code(idea):
|
|
142 |
"""
|
143 |
|
144 |
# Load the code generation model
|
145 |
-
model_name =
|
146 |
try:
|
147 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
148 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
149 |
except EnvironmentError as e:
|
150 |
-
return f
|
151 |
|
152 |
# Generate the code
|
153 |
input_text = f"""
|
@@ -171,48 +259,98 @@ def generate_code(idea):
|
|
171 |
|
172 |
return generated_code
|
173 |
|
|
|
174 |
# Streamlit App
|
175 |
st.title("CodeCraft: Your AI-Powered Development Toolkit")
|
176 |
|
177 |
-
#
|
178 |
-
st.
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
#
|
200 |
-
st.
|
201 |
-
|
202 |
-
if st.button("
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import subprocess
|
4 |
from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer
|
5 |
import black
|
6 |
+
from pylint import epylint as lint
|
7 |
+
|
8 |
+
PROJECT_ROOT = "projects"
|
9 |
|
10 |
# Define functions for each feature
|
11 |
|
|
|
20 |
The chatbot's response.
|
21 |
"""
|
22 |
# Load the GPT-2 model which is compatible with AutoModelForCausalLM
|
23 |
+
model_name = "gpt2"
|
24 |
try:
|
25 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
26 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
27 |
+
generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
|
28 |
except EnvironmentError as e:
|
29 |
+
return f"Error loading model: {e}"
|
30 |
|
31 |
# Truncate input text to avoid exceeding the model's maximum length
|
32 |
max_input_length = 900
|
|
|
35 |
input_ids = input_ids[:, :max_input_length]
|
36 |
|
37 |
# Generate chatbot response
|
38 |
+
outputs = model.generate(
|
39 |
+
input_ids, max_new_tokens=50, num_return_sequences=1, do_sample=True
|
40 |
+
)
|
41 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
42 |
return response
|
43 |
|
44 |
+
|
45 |
# 2. Terminal
|
46 |
+
def terminal_interface(command, project_name=None):
|
47 |
"""Executes commands in the terminal.
|
48 |
|
49 |
Args:
|
50 |
command: User's command.
|
51 |
+
project_name: Name of the project workspace to add installed packages.
|
52 |
|
53 |
Returns:
|
54 |
The terminal output.
|
|
|
57 |
try:
|
58 |
process = subprocess.run(command.split(), capture_output=True, text=True)
|
59 |
output = process.stdout
|
60 |
+
|
61 |
+
# If the command is to install a package, update the workspace
|
62 |
+
if "install" in command and project_name:
|
63 |
+
requirements_path = os.path.join(PROJECT_ROOT, project_name, "requirements.txt")
|
64 |
+
with open(requirements_path, "a") as req_file:
|
65 |
+
package_name = command.split()[-1]
|
66 |
+
req_file.write(f"{package_name}\n")
|
67 |
except Exception as e:
|
68 |
+
output = f"Error: {e}"
|
69 |
return output
|
70 |
|
71 |
+
|
72 |
# 3. Code Editor
|
73 |
def code_editor_interface(code):
|
74 |
"""Provides code completion, formatting, and linting in the code editor.
|
|
|
87 |
|
88 |
# Lint code using pylint
|
89 |
try:
|
90 |
+
(pylint_stdout, pylint_stderr) = lint.py_run(code, return_std=True)
|
91 |
+
lint_message = pylint_stdout.getvalue()
|
|
|
92 |
except Exception as e:
|
93 |
lint_message = f"Pylint error: {e}"
|
94 |
|
95 |
return formatted_code, lint_message
|
96 |
|
97 |
+
|
98 |
# 4. Workspace
|
99 |
def workspace_interface(project_name):
|
100 |
"""Manages projects, files, and resources in the workspace.
|
|
|
105 |
Returns:
|
106 |
Project creation status.
|
107 |
"""
|
108 |
+
project_path = os.path.join(PROJECT_ROOT, project_name)
|
109 |
# Create project directory
|
110 |
try:
|
111 |
+
os.makedirs(project_path)
|
112 |
+
requirements_path = os.path.join(project_path, "requirements.txt")
|
113 |
+
with open(requirements_path, "w") as req_file:
|
114 |
+
req_file.write("") # Initialize an empty requirements.txt file
|
115 |
+
status = f'Project "{project_name}" created successfully.'
|
116 |
except FileExistsError:
|
117 |
+
status = f'Project "{project_name}" already exists.'
|
118 |
return status
|
119 |
|
120 |
+
def add_code_to_workspace(project_name, code, file_name):
|
121 |
+
"""Adds selected code files to the workspace.
|
122 |
+
|
123 |
+
Args:
|
124 |
+
project_name: Name of the project.
|
125 |
+
code: Code to be added.
|
126 |
+
file_name: Name of the file to be created.
|
127 |
+
|
128 |
+
Returns:
|
129 |
+
File creation status.
|
130 |
+
"""
|
131 |
+
project_path = os.path.join(PROJECT_ROOT, project_name)
|
132 |
+
file_path = os.path.join(project_path, file_name)
|
133 |
+
|
134 |
+
try:
|
135 |
+
with open(file_path, "w") as code_file:
|
136 |
+
code_file.write(code)
|
137 |
+
status = f'File "{file_name}" added to project "{project_name}" successfully.'
|
138 |
+
except Exception as e:
|
139 |
+
status = f"Error: {e}"
|
140 |
+
return status
|
141 |
+
|
142 |
+
|
143 |
# 5. AI-Infused Tools
|
144 |
|
145 |
# Define custom AI-powered tools using Hugging Face models
|
146 |
|
147 |
+
|
148 |
# Example: Text summarization tool
|
149 |
def summarize_text(text):
|
150 |
"""Summarizes a given text using a Hugging Face model.
|
|
|
156 |
Summarized text.
|
157 |
"""
|
158 |
# Load the summarization model
|
159 |
+
model_name = "facebook/bart-large-cnn"
|
160 |
try:
|
161 |
+
summarizer = pipeline("summarization", model=model_name)
|
162 |
except EnvironmentError as e:
|
163 |
+
return f"Error loading model: {e}"
|
164 |
|
165 |
# Truncate input text to avoid exceeding the model's maximum length
|
166 |
max_input_length = 1024
|
|
|
169 |
inputs = text[:max_input_length]
|
170 |
|
171 |
# Generate summary
|
172 |
+
summary = summarizer(inputs, max_length=100, min_length=30, do_sample=False)[0][
|
173 |
+
"summary_text"
|
174 |
+
]
|
175 |
return summary
|
176 |
|
177 |
+
# Example: Sentiment analysis tool
|
178 |
+
def sentiment_analysis(text):
|
179 |
+
"""Performs sentiment analysis on a given text using a Hugging Face model.
|
180 |
+
|
181 |
+
Args:
|
182 |
+
text: Text to be analyzed.
|
183 |
+
|
184 |
+
Returns:
|
185 |
+
Sentiment analysis result.
|
186 |
+
"""
|
187 |
+
# Load the sentiment analysis model
|
188 |
+
model_name = "distilbert-base-uncased-finetuned-sst-2-english"
|
189 |
+
try:
|
190 |
+
analyzer = pipeline("sentiment-analysis", model=model_name)
|
191 |
+
except EnvironmentError as e:
|
192 |
+
return f"Error loading model: {e}"
|
193 |
+
|
194 |
+
# Perform sentiment analysis
|
195 |
+
result = analyzer(text)[0]
|
196 |
+
return result
|
197 |
+
|
198 |
+
# Example: Text translation tool
|
199 |
+
def translate_text(text, target_language="fr"):
|
200 |
+
"""Translates a given text to the target language using a Hugging Face model.
|
201 |
+
|
202 |
+
Args:
|
203 |
+
text: Text to be translated.
|
204 |
+
target_language: The language to translate the text to.
|
205 |
+
|
206 |
+
Returns:
|
207 |
+
Translated text.
|
208 |
+
"""
|
209 |
+
# Load the translation model
|
210 |
+
model_name = f"Helsinki-NLP/opus-mt-en-{target_language}"
|
211 |
+
try:
|
212 |
+
translator = pipeline("translation", model=model_name)
|
213 |
+
except EnvironmentError as e:
|
214 |
+
return f"Error loading model: {e}"
|
215 |
+
|
216 |
+
# Translate text
|
217 |
+
translated_text = translator(text)[0]["translation_text"]
|
218 |
+
return translated_text
|
219 |
+
|
220 |
+
|
221 |
# 6. Code Generation
|
222 |
def generate_code(idea):
|
223 |
"""Generates code based on a given idea using the EleutherAI/gpt-neo-2.7B model.
|
|
|
230 |
"""
|
231 |
|
232 |
# Load the code generation model
|
233 |
+
model_name = "EleutherAI/gpt-neo-2.7B"
|
234 |
try:
|
235 |
model = AutoModelForCausalLM.from_pretrained(model_name)
|
236 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
237 |
except EnvironmentError as e:
|
238 |
+
return f"Error loading model: {e}"
|
239 |
|
240 |
# Generate the code
|
241 |
input_text = f"""
|
|
|
259 |
|
260 |
return generated_code
|
261 |
|
262 |
+
|
263 |
# Streamlit App
|
264 |
st.title("CodeCraft: Your AI-Powered Development Toolkit")
|
265 |
|
266 |
+
# Sidebar navigation
|
267 |
+
st.sidebar.title("Navigation")
|
268 |
+
app_mode = st.sidebar.selectbox("Choose the app mode", ["Tool Box", "Workspace Chat App"])
|
269 |
+
|
270 |
+
if app_mode == "Tool Box":
|
271 |
+
# Tool Box
|
272 |
+
st.header("AI-Powered Tools")
|
273 |
+
|
274 |
+
# Chat Interface
|
275 |
+
st.subheader("Chat with CodeCraft")
|
276 |
+
chat_input = st.text_area("Enter your message:")
|
277 |
+
if st.button("Send"):
|
278 |
+
chat_response = chat_interface(chat_input)
|
279 |
+
st.write(f"CodeCraft: {chat_response}")
|
280 |
+
|
281 |
+
# Terminal Interface
|
282 |
+
st.subheader("Terminal")
|
283 |
+
terminal_input = st.text_input("Enter a command:")
|
284 |
+
if st.button("Run"):
|
285 |
+
terminal_output = terminal_interface(terminal_input)
|
286 |
+
st.code(terminal_output, language="bash")
|
287 |
+
|
288 |
+
# Code Editor Interface
|
289 |
+
st.subheader("Code Editor")
|
290 |
+
code_editor = st.text_area("Write your code:", height=300)
|
291 |
+
if st.button("Format & Lint"):
|
292 |
+
formatted_code, lint_message = code_editor_interface(code_editor)
|
293 |
+
st.code(formatted_code, language="python")
|
294 |
+
st.info(lint_message)
|
295 |
+
|
296 |
+
# Text Summarization Tool
|
297 |
+
st.subheader("Summarize Text")
|
298 |
+
text_to_summarize = st.text_area("Enter text to summarize:")
|
299 |
+
if st.button("Summarize"):
|
300 |
+
summary = summarize_text(text_to_summarize)
|
301 |
+
st.write(f"Summary: {summary}")
|
302 |
+
|
303 |
+
# Sentiment Analysis Tool
|
304 |
+
st.subheader("Sentiment Analysis")
|
305 |
+
sentiment_text = st.text_area("Enter text for sentiment analysis:")
|
306 |
+
if st.button("Analyze Sentiment"):
|
307 |
+
sentiment = sentiment_analysis(sentiment_text)
|
308 |
+
st.write(f"Sentiment: {sentiment}")
|
309 |
+
|
310 |
+
# Text Translation Tool
|
311 |
+
st.subheader("Translate Text")
|
312 |
+
translation_text = st.text_area("Enter text to translate:")
|
313 |
+
target_language = st.text_input("Enter target language code (e.g., 'fr' for French):")
|
314 |
+
if st.button("Translate"):
|
315 |
+
translated_text = translate_text(translation_text, target_language)
|
316 |
+
st.write(f"Translated Text: {translated_text}")
|
317 |
+
|
318 |
+
# Code Generation
|
319 |
+
st.subheader("Code Generation")
|
320 |
+
code_idea = st.text_input("Enter your code idea:")
|
321 |
+
if st.button("Generate Code"):
|
322 |
+
generated_code = generate_code(code_idea)
|
323 |
+
st.code(generated_code, language="python")
|
324 |
+
|
325 |
+
elif app_mode == "Workspace Chat App":
|
326 |
+
# Workspace Chat App
|
327 |
+
st.header("Workspace Chat App")
|
328 |
+
|
329 |
+
# Project Workspace Creation
|
330 |
+
st.subheader("Create a New Project")
|
331 |
+
project_name = st.text_input("Enter project name:")
|
332 |
+
if st.button("Create Project"):
|
333 |
+
workspace_status = workspace_interface(project_name)
|
334 |
+
st.success(workspace_status)
|
335 |
+
|
336 |
+
# Add Code to Workspace
|
337 |
+
st.subheader("Add Code to Workspace")
|
338 |
+
code_to_add = st.text_area("Enter code to add to workspace:")
|
339 |
+
file_name = st.text_input("Enter file name (e.g., 'app.py'):")
|
340 |
+
if st.button("Add Code"):
|
341 |
+
add_code_status = add_code_to_workspace(project_name, code_to_add, file_name)
|
342 |
+
st.success(add_code_status)
|
343 |
+
|
344 |
+
# Terminal Interface with Project Context
|
345 |
+
st.subheader("Terminal (Workspace Context)")
|
346 |
+
terminal_input = st.text_input("Enter a command within the workspace:")
|
347 |
+
if st.button("Run Command"):
|
348 |
+
terminal_output = terminal_interface(terminal_input, project_name)
|
349 |
+
st.code(terminal_output, language="bash")
|
350 |
+
|
351 |
+
# Chat Interface for Guidance
|
352 |
+
st.subheader("Chat with CodeCraft for Guidance")
|
353 |
+
chat_input = st.text_area("Enter your message for guidance:")
|
354 |
+
if st.button("Get Guidance"):
|
355 |
+
chat_response = chat_interface(chat_input)
|
356 |
+
st.write(f"CodeCraft: {chat_response}")
|