acecalisto3 commited on
Commit
d6eab59
·
verified ·
1 Parent(s): 4c266dd

Create agents.py

Browse files
Files changed (1) hide show
  1. agents.py +160 -0
agents.py ADDED
@@ -0,0 +1,160 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
2
+ from huggingface_hub import HfApi
3
+ import re
4
+ from typing import List, Dict
5
+ import subprocess
6
+ import os
7
+ import black
8
+ from pylint import lint
9
+ from io import StringIO
10
+ import sys
11
+
12
+ class TextGenerationTool:
13
+ def __init__(self, llm: str):
14
+ self.llm = llm
15
+ self.tokenizer = AutoTokenizer.from_pretrained(llm)
16
+ self.model = AutoModelForCausalLM.from_pretrained(llm)
17
+
18
+ def generate_text(self, prompt: str, max_length: int = 50) -> str:
19
+ inputs = self.tokenizer(prompt, return_tensors="pt")
20
+ outputs = self.model.generate(**inputs, max_length=max_length)
21
+ return self.tokenizer.decode(outputs[0], skip_special_tokens=True)
22
+
23
+ class AIAgent:
24
+ def __init__(self, name: str, description: str, skills: List[str], llm: str):
25
+ self.name = name
26
+ self.description = description
27
+ self.skills = skills
28
+ self.text_gen_tool = TextGenerationTool(llm)
29
+ self._hf_api = HfApi() # Initialize HfApi here
30
+
31
+ def generate_agent_response(self, prompt: str) -> str:
32
+ return self.text_gen_tool.generate_text(prompt)
33
+
34
+ def create_agent_prompt(self) -> str:
35
+ skills_str = '\n'.join([f"* {skill}" for skill in self.skills])
36
+ agent_prompt = f"""
37
+ As an elite expert developer, my name is {self.name}. I possess a comprehensive understanding of the following areas:
38
+ {skills_str}
39
+ I am confident that I can leverage my expertise to assist you in developing and deploying cutting-edge web applications. Please feel free to ask any questions or present any challenges you may encounter.
40
+ """
41
+ return agent_prompt
42
+
43
+ def autonomous_build(self, chat_history: List[tuple[str, str]], workspace_projects: Dict[str, Dict],
44
+ project_name: str, selected_model: str, hf_token: str) -> tuple[str, str]:
45
+ summary = "Chat History:\n" + "\n".join([f"User: {u}\nAgent: {a}" for u, a in chat_history])
46
+ summary += "\n\nWorkspace Projects:\n" + "\n.join([f"{p}: {details}" for p, details in workspace_projects.items()])
47
+ next_step = "Based on the current state, the next logical step is to implement the main application logic."
48
+ return summary, next_step
49
+
50
+ def deploy_built_space_to_hf(self, project_name: str) -> str:
51
+ space_content = generate_space_content(project_name)
52
+ repository = self._hf_api.create_repo(
53
+ repo_id=project_name,
54
+ private=True,
55
+ token=hf_token,
56
+ exist_ok=True,
57
+ space_sdk="streamlit"
58
+ )
59
+ self._hf_api.upload_file(
60
+ path_or_fileobj=space_content,
61
+ path_in_repo="app.py",
62
+ repo_id=project_name,
63
+ repo_type="space",
64
+ token=hf_token
65
+ )
66
+ return repository.name
67
+
68
+ def has_valid_hf_token(self) -> bool:
69
+ return self._hf_api.whoami(token=hf_token) is not None
70
+
71
+ def process_input(input_text: str) -> str:
72
+ chatbot = pipeline("text-generation", model="microsoft/DialoGPT-medium", tokenizer="microsoft/DialoGPT-medium", clean_up_tokenization_spaces=True)
73
+ response = chatbot(input_text, max_length=50, num_return_sequences=1)[0]['generated_text']
74
+ return response
75
+
76
+ def run_code(code: str) -> str:
77
+ try:
78
+ result = subprocess.run(code, shell=True, capture_output=True, text=True)
79
+ return result.stdout
80
+ except Exception as e:
81
+ return str(e)
82
+
83
+ def workspace_interface(project_name: str) -> str:
84
+ project_path = os.path.join(PROJECT_ROOT, project_name)
85
+ if not os.path.exists(project_path):
86
+ os.makedirs(project_path)
87
+ st.session_state.workspace_projects[project_name] = {'files': []}
88
+ return f"Project '{project_name}' created successfully."
89
+ else:
90
+ return f"Project '{project_name}' already exists."
91
+
92
+ def add_code_to_workspace(project_name: str, code: str, file_name: str) -> str:
93
+ project_path = os.path.join(PROJECT_ROOT, project_name)
94
+ if not os.path.exists(project_path):
95
+ return f"Project '{project_name}' does not exist."
96
+
97
+ file_path = os.path.join(project_path, file_name)
98
+ with open(file_path, "w") as file:
99
+ file.write(code)
100
+ st.session_state.workspace_projects[project_name]['files'].append(file_name)
101
+ return f"Code added to '{file_name}' in project '{project_name}'."
102
+
103
+ def display_chat_history(chat_history: List[tuple[str, str]]) -> str:
104
+ return "\n".join([f"User: {u}\nAgent: {a}" for u, a in chat_history])
105
+
106
+ def display_workspace_projects(workspace_projects: Dict[str, Dict]) -> str:
107
+ return "\n".join([f"{p}: {details}" for p, details in workspace_projects.items()])
108
+
109
+ def generate_space_content(project_name: str) -> str:
110
+ # Logic to generate the Streamlit app content based on project_name
111
+ # ... (This is where you'll need to implement the actual code generation)
112
+ return "import streamlit as st\nst.title('My Streamlit App')\nst.write('Hello, world!')"
113
+
114
+ def analyze_code(code: str) -> List[str]:
115
+ hints = []
116
+
117
+ # Example pointer: Suggest using list comprehensions
118
+ if re.search(r'for .* in .*:\n\s+.*\.append\(', code):
119
+ hints.append("Consider using a list comprehension instead of a loop for appending to a list.")
120
+
121
+ # Example pointer: Recommend using f-strings for string formatting
122
+ if re.search(r'\".*\%s\"|\'.*\%s\'', code) or re.search(r'\".*\%d\"|\'.*\%d\'', code):
123
+ hints.append("Consider using f-strings for cleaner and more efficient string formatting.")
124
+
125
+ # Example pointer: Avoid using global variables
126
+ if re.search(r'\bglobal\b', code):
127
+ hints.append("Avoid using global variables. Consider passing parameters or using classes.")
128
+
129
+ # Example pointer: Recommend using `with` statement for file operations
130
+ if re.search(r'open\(.+\)', code) and not re.search(r'with open\(.+\)', code):
131
+ hints.append("Consider using the `with` statement when opening files to ensure proper resource management.")
132
+
133
+ return hints
134
+
135
+ def get_code_completion(prompt: str) -> str:
136
+ # Generate code completion based on the current code input
137
+ # Use max_new_tokens instead of max_length
138
+ completions = code_generator(prompt, max_new_tokens=50, num_return_sequences=1)
139
+ return completions[0]['generated_text']
140
+
141
+ def lint_code(code: str) -> List[str]:
142
+ # Capture pylint output
143
+ pylint_output = StringIO()
144
+ sys.stdout = pylint_output
145
+
146
+ # Run pylint on the provided code
147
+ lint.Run(['--from-stdin'], do_exit=False, input=code)
148
+
149
+ # Reset stdout
150
+ sys.stdout = sys.__stdout__
151
+
152
+ # Extract pylint messages
153
+ messages = pylint_output.getvalue().splitlines()
154
+
155
+ return messages
156
+
157
+ def format_code(code: str) -> str:
158
+ # Format code using Black
159
+ formatted_code = black.format_str(code, mode=black.FileMode())
160
+ return formatted_code