acecalisto3 commited on
Commit
aba74fe
1 Parent(s): 401a5f9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -25
app.py CHANGED
@@ -1,11 +1,13 @@
1
  import os
2
  import subprocess
 
3
  import streamlit as st
4
  import black
5
  from pylint import lint
6
  from io import StringIO
7
  from mixtral import InstructModel # Import Mixtral Instruct
8
 
 
9
  HUGGING_FACE_REPO_URL = "https://huggingface.co/spaces/acecalisto3/DevToolKit"
10
  PROJECT_ROOT = "projects"
11
  AGENT_DIRECTORY = "agents"
@@ -81,6 +83,15 @@ def create_agent_from_text(name, text):
81
  save_agent_to_file(agent)
82
  return agent.create_agent_prompt()
83
 
 
 
 
 
 
 
 
 
 
84
  def chat_interface_with_agent(input_text, agent_name):
85
  agent_prompt = load_agent_prompt(agent_name)
86
  if agent_prompt is None:
@@ -141,28 +152,32 @@ def code_editor_interface(code):
141
  formatted_code = black.format_str(code, mode=black.FileMode())
142
  except black.NothingChanged:
143
  formatted_code = code
 
 
 
144
  result = StringIO()
145
  sys.stdout = result
146
  sys.stderr = result
147
- (pylint_stdout, pylint_stderr) = lint.py_run(code, return_std=True)
148
- sys.stdout = sys.__stdout__
149
- sys.stderr = sys.__stderr__
150
- lint_message = pylint_stdout.getvalue() + pylint_stderr.getvalue()
151
- st.session_state.current_state['toolbox']['formatted_code'] = formatted_code
152
- st.session_state.current_state['toolbox']['lint_message'] = lint_message
 
 
153
  return formatted_code, lint_message
154
 
155
  def translate_code(code, input_language, output_language):
156
  try:
157
- model = InstructModel() # Initialize Mixtral Instruct model
 
 
 
158
  except EnvironmentError as e:
159
- return f"Error loading model: {e}"
160
-
161
- prompt = f"Translate the following {input_language} code to {output_language}:\n\n{code}"
162
- translated_code = model.generate_response(prompt)
163
- st.session_state.current_state['toolbox']['translated_code'] = translated_code
164
- return translated_code
165
-
166
  def generate_code(code_idea):
167
  try:
168
  model = InstructModel() # Initialize Mixtral Instruct model
@@ -175,17 +190,16 @@ def generate_code(code_idea):
175
  return generated_code
176
 
177
  def commit_and_push_changes(commit_message):
178
- """Commits and pushes changes to the Hugging Face repository."""
179
- commands = [
180
- "git add .",
181
- f"git commit -m '{commit_message}'",
182
- "git push"
183
- ]
184
- for command in commands:
185
- result = subprocess.run(command, shell=True, capture_output=True, text=True)
186
- if result.returncode != 0:
187
- st.error(f"Error executing command '{command}': {result.stderr}")
188
- break
189
 
190
  # Streamlit App
191
  st.title("AI Agent Creator")
 
1
  import os
2
  import subprocess
3
+ import sys # Add sys import
4
  import streamlit as st
5
  import black
6
  from pylint import lint
7
  from io import StringIO
8
  from mixtral import InstructModel # Import Mixtral Instruct
9
 
10
+
11
  HUGGING_FACE_REPO_URL = "https://huggingface.co/spaces/acecalisto3/DevToolKit"
12
  PROJECT_ROOT = "projects"
13
  AGENT_DIRECTORY = "agents"
 
83
  save_agent_to_file(agent)
84
  return agent.create_agent_prompt()
85
 
86
+ def chat_interface(input_text):
87
+ """Handles chat interactions without a specific agent."""
88
+ try:
89
+ model = InstructModel()
90
+ response = model.generate_response(f"User: {input_text}\nAI:")
91
+ return response
92
+ except EnvironmentError as e:
93
+ return f"Error communicating with AI: {e}"
94
+
95
  def chat_interface_with_agent(input_text, agent_name):
96
  agent_prompt = load_agent_prompt(agent_name)
97
  if agent_prompt is None:
 
152
  formatted_code = black.format_str(code, mode=black.FileMode())
153
  except black.NothingChanged:
154
  formatted_code = code
155
+ except Exception as e:
156
+ return None, f"Error formatting code with black: {e}"
157
+
158
  result = StringIO()
159
  sys.stdout = result
160
  sys.stderr = result
161
+ try:
162
+ (pylint_stdout, pylint_stderr) = lint.py_run(code, return_std=True)
163
+ lint_message = pylint_stdout.getvalue() + pylint_stderr.getvalue()
164
+ except Exception as e:
165
+ return None, f"Error linting code with pylint: {e}"
166
+ finally:
167
+ sys.stdout = sys.__stdout__
168
+ sys.stderr = sys.__stderr__
169
  return formatted_code, lint_message
170
 
171
  def translate_code(code, input_language, output_language):
172
  try:
173
+ model = InstructModel()
174
+ prompt = f"Translate the following {input_language} code to {output_language}:\n\n{code}"
175
+ translated_code = model.generate_response(prompt)
176
+ return translated_code
177
  except EnvironmentError as e:
178
+ return f"Error loading model or translating code: {e}"
179
+ except Exception as e: # Catch other potential errors during translation.
180
+ return f"An unexpected error occurred during code translation: {e}"
 
 
 
 
181
  def generate_code(code_idea):
182
  try:
183
  model = InstructModel() # Initialize Mixtral Instruct model
 
190
  return generated_code
191
 
192
  def commit_and_push_changes(commit_message):
193
+ """Commits and pushes changes to the Hugging Face repository (needs improvement)."""
194
+ try:
195
+ # Add error checking for git repository existence.
196
+ subprocess.run(["git", "add", "."], check=True, capture_output=True, text=True)
197
+ subprocess.run(["git", "commit", "-m", commit_message], check=True, capture_output=True, text=True)
198
+ subprocess.run(["git", "push"], check=True, capture_output=True, text=True)
199
+ except subprocess.CalledProcessError as e:
200
+ st.error(f"Git command failed: {e.stderr}")
201
+ except FileNotFoundError:
202
+ st.error("Git not found. Please ensure Git is installed and configured.")
 
203
 
204
  # Streamlit App
205
  st.title("AI Agent Creator")