acecalisto3 commited on
Commit
ded0f49
1 Parent(s): 5fa6d40

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +548 -290
app.py CHANGED
@@ -1,297 +1,555 @@
1
- import requests
2
- from bs4 import BeautifulSoup
3
- import pandas as pd
4
- import numpy as np
5
- import matplotlib.pyplot as plt
6
- import seaborn as sns
7
- from datetime import datetime
8
- from nltk.corpus import stopwords
9
- from nltk.stem import WordNetLemmatizer
10
- from nltk.tokenize import word_tokenize
11
- from gensim.models import LdaModel
12
- from gensim.corpora import Dictionary
13
- from textblob import TextBlob
14
- from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
15
- import networkx as nx
16
- from sklearn.model_selection import train_test_split
17
- from sklearn.linear_model import LogisticRegression
18
- from sklearn.ensemble import RandomForestClassifier
19
- from sklearn.metrics import accuracy_score, classification_report, roc_auc_score
20
- from sklearn.preprocessing import StandardScaler
21
- from sklearn.pipeline import Pipeline
22
- from sklearn.feature_extraction.text import TfidfVectorizer
23
- from scipy import linalg
24
- import plotly.graph_objects as go
25
- from collections import Counter
26
- import warnings
27
- warnings.filterwarnings("ignore")
28
-
29
- # Set up logging
30
- import logging
31
- logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
32
-
33
- # Function to fetch HTML content from GitHub issue pages
34
- def fetch_issue_data(username, repository, start_page, end_page):
35
- issues_data = []
36
- for page in range(start_page, end_page + 1):
37
- url = f"https://github.com/{username}/{repository}/issues?page={page}"
38
- response = requests.get(url)
39
- soup = BeautifulSoup(response.content, 'html.parser')
40
- issue_elements = soup.find_all('div', class_='flex-shrink-0')
41
- for issue_element in issue_elements:
42
- issue_link = issue_element.find('a', class_='Link--primary')['href']
43
- issue_url = f"https://github.com{issue_link}"
44
- issue_data = fetch_issue_details(issue_url)
45
- issues_data.append(issue_data)
46
- return issues_data
47
-
48
- # Function to fetch details of a specific issue
49
- def fetch_issue_details(issue_url):
50
- response = requests.get(issue_url)
51
- soup = BeautifulSoup(response.content, 'html.parser')
52
- issue_title = soup.find('h1', class_='gh-header-title').text.strip()
53
- issue_body = soup.find('div', class_='markdown-body').text.strip()
54
- issue_created_at = soup.find('relative-time')['datetime']
55
- issue_closed_at = soup.find('relative-time', class_='no-wrap')
56
- if issue_closed_at:
57
- issue_closed_at = issue_closed_at['datetime']
58
- else:
59
- issue_closed_at = None
60
- issue_author = soup.find('a', class_='author').text.strip()
61
- issue_assignee = soup.find('a', class_='Link--muted')
62
- if issue_assignee:
63
- issue_assignee = issue_assignee.text.strip()
64
- else:
65
- issue_assignee = None
66
- return {
67
- 'title': issue_title,
68
- 'body': issue_body,
69
- 'created_at': issue_created_at,
70
- 'closed_at': issue_closed_at,
71
- 'author': issue_author,
72
- 'assignee': issue_assignee
73
  }
74
 
75
- # Function to clean and structure the data
76
- def clean_and_structure_data(issues_data):
77
- df = pd.DataFrame(issues_data)
78
-
79
- # Check if 'created_at' column exists
80
- if 'created_at' in df.columns:
81
- df['created_at'] = pd.to_datetime(df['created_at'])
82
- else:
83
- logging.error("The 'created_at' column is missing from the dataframe.")
84
- df['created_at'] = pd.NaT # or use pd.to_datetime('now') for current time
85
-
86
- # Check if 'closed_at' column exists
87
- if 'closed_at' in df.columns:
88
- df['closed_at'] = pd.to_datetime(df['closed_at'])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  else:
90
- df['closed_at'] = pd.NaT
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
91
 
92
- df['resolution_time'] = (df['closed_at'] - df['created_at']).dt.days
93
- df['resolution_time'] = df['resolution_time'].fillna(-1)
94
- df['is_closed'] = (df['closed_at'].notna()).astype(int)
95
- return df
96
-
97
- # Function for exploratory data analysis (EDA)
98
- def perform_eda(df):
99
- # Descriptive statistics
100
- print("Descriptive Statistics:")
101
- print(df.describe())
102
-
103
- # Visualizations
104
- plt.figure(figsize=(10, 6))
105
- sns.histplot(df['resolution_time'], kde=True)
106
- plt.title('Distribution of Issue Resolution Time')
107
- plt.xlabel('Resolution Time (Days)')
108
- plt.ylabel('Frequency')
109
- plt.show()
110
-
111
- # Trend analysis
112
- df['created_at_month'] = df['created_at'].dt.month
113
- plt.figure(figsize=(10, 6))
114
- sns.lineplot(x='created_at_month', y='resolution_time', data=df)
115
- plt.title('Trend of Issue Resolution Time Over Months')
116
- plt.xlabel('Month')
117
- plt.ylabel('Resolution Time (Days)')
118
- plt.show()
119
-
120
- # Top Authors and Assignees
121
- top_authors = df['author'].value_counts().nlargest(10)
122
- top_assignees = df['assignee'].value_counts().nlargest(10)
123
- print("\nTop 10 Authors:")
124
- print(top_authors)
125
- print("\nTop 10 Assignees:")
126
- print(top_assignees)
127
-
128
- # Function for text analysis using NLP
129
- def analyze_text_content(df):
130
- # Text preprocessing
131
- stop_words = set(stopwords.words('english'))
132
- lemmatizer = WordNetLemmatizer()
133
- df['processed_body'] = df['body'].apply(lambda text: ' '.join([lemmatizer.lemmatize(word) for word in word_tokenize(text) if word.lower() not in stop_words]))
134
-
135
- # Topic modeling
136
- dictionary = Dictionary([word_tokenize(text) for text in df['processed_body']])
137
- corpus = [dictionary.doc2bow(word_tokenize(text)) for text in df['processed_body']]
138
- lda_model = LdaModel(corpus, num_topics=5, id2word=dictionary)
139
- print("Top 5 Topics:")
140
- for topic in lda_model.print_topics(num_words=5):
141
- print(topic)
142
-
143
- # Sentiment analysis
144
- analyzer = SentimentIntensityAnalyzer()
145
- df['sentiment'] = df['body'].apply(lambda text: analyzer.polarity_scores(text)['compound'])
146
- print("Sentiment Analysis:")
147
- print(df['sentiment'].describe())
148
-
149
- # Word Cloud for Common Words
150
- from wordcloud import WordCloud
151
- all_words = ' '.join([text for text in df['processed_body']])
152
- wordcloud = WordCloud(width=800, height=400, background_color='white').generate(all_words)
153
- plt.figure(figsize=(10, 6), facecolor=None)
154
- plt.imshow(wordcloud)
155
- plt.axis("off")
156
- plt.tight_layout(pad=0)
157
- plt.show()
158
-
159
- # Function to create a network graph of issues, authors, and assignees
160
- def create_network_graph(df):
161
- graph = nx.Graph()
162
- for index, row in df.iterrows():
163
- graph.add_node(row['title'], type='issue')
164
- graph.add_node(row['author'], type='author')
165
- if row['assignee']:
166
- graph.add_node(row['assignee'], type='assignee')
167
- graph.add_edge(row['title'], row['author'])
168
- if row['assignee']:
169
- graph.add_edge(row['title'], row['assignee'])
170
-
171
- # Interactive Network Graph with Plotly
172
- pos = nx.spring_layout(graph, k=0.5)
173
- edge_x = []
174
- edge_y = []
175
- for edge in graph.edges():
176
- x0, y0 = pos[edge[0]]
177
- x1, y1 = pos[edge[1]]
178
- edge_x.append([x0, x1, None])
179
- edge_y.append([y0, y1, None])
180
-
181
- edge_trace = go.Scatter(
182
- x=edge_x,
183
- y=edge_y,
184
- line=dict(width=0.5, color='#888'),
185
- hoverinfo='none',
186
- mode='lines'
187
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
188
 
189
- node_x = []
190
- node_y = []
191
- for node in graph.nodes():
192
- x, y = pos[node]
193
- node_x.append(x)
194
- node_y.append(y)
195
-
196
- node_trace = go.Scatter(
197
- x=node_x,
198
- y=node_y,
199
- mode='markers',
200
- marker=dict(
201
- color=[],
202
- size=10,
203
- line=dict(width=2, color='black')
204
- ),
205
- text=[],
206
- hoverinfo='text'
207
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
208
 
209
- # Set node colors based on type
210
- node_colors = []
211
- for node in graph.nodes():
212
- if graph.nodes[node]['type'] == 'issue':
213
- node_colors.append('red')
214
- elif graph.nodes[node]['type'] == 'author':
215
- node_colors.append('blue')
216
- else:
217
- node_colors.append('green')
218
-
219
- # Set node labels
220
- node_labels = []
221
- for node in graph.nodes():
222
- node_labels.append(node)
223
-
224
- node_trace.marker.color = node_colors
225
- node_trace.text = node_labels
226
-
227
- # Create the figure
228
- fig = go.Figure(data=[edge_trace, node_trace],
229
- layout=go.Layout(
230
- title="GitHub Issue Network Graph",
231
- showlegend=False,
232
- hovermode='closest',
233
- margin=dict(b=20, l=5, r=5, t=40),
234
- xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
235
- yaxis=dict(showgrid=False, zeroline=False, showticklabels=False)
236
- )
237
- )
238
-
239
- fig.show()
240
-
241
- # Function to build a predictive model for issue resolution time
242
- def build_predictive_model(df):
243
- # Feature engineering
244
- df['created_at_day'] = df['created_at'].dt.day
245
- df['created_at_weekday'] = df['created_at'].dt.weekday
246
- df['created_at_hour'] = df['created_at'].dt.hour
247
- df['author_encoded'] = df['author'].astype('category').cat.codes
248
- df['assignee_encoded'] = df['assignee'].astype('category').cat.codes
249
-
250
- # Select features and target variable
251
- features = ['created_at_day', 'created_at_weekday', 'created_at_hour', 'author_encoded', 'assignee_encoded', 'sentiment']
252
- target = 'resolution_time'
253
-
254
- # Split data into training and testing sets
255
- X_train, X_test, y_train, y_test = train_test_split(df[features], df[target], test_size=0.2, random_state=42)
256
-
257
- # Create a pipeline for feature scaling and model training
258
- pipeline = Pipeline([
259
- ('scaler', StandardScaler()),
260
- ('model', RandomForestClassifier(random_state=42))
261
- ])
262
-
263
- # Train the model
264
- pipeline.fit(X_train, y_train)
265
-
266
- # Evaluate the model
267
- y_pred = pipeline.predict(X_test)
268
- accuracy = accuracy_score(y_test, y_pred)
269
- print("Accuracy:", accuracy)
270
- print(classification_report(y_test, y_pred))
271
-
272
- # Make predictions on new data
273
- # ...
274
-
275
- # Main function
276
- if __name__ == "__main__":
277
- # Replace with your GitHub username and repository name
278
- username = "miagiii"
279
- repository = "miagiii"
280
-
281
- # Fetch issue data from GitHub
282
- issues_data = fetch_issue_data(username, repository, 1, 10) # Fetch issues from pages 1 to 10
283
-
284
- # Clean and structure the data
285
- df = clean_and_structure_data(issues_data)
286
-
287
- # Perform exploratory data analysis (EDA)
288
- perform_eda(df)
289
-
290
- # Analyze text content using NLP
291
- analyze_text_content(df)
292
-
293
- # Create a network graph of issues, authors, and assignees
294
- create_network_graph(df)
295
-
296
- # Build a predictive model for issue resolution time
297
- build_predictive_model(df)
 
1
+ import subprocess
2
+ import streamlit as st
3
+ from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer, AutoModel, RagRetriever, AutoModelForSeq2SeqLM
4
+ import black
5
+ from pylint import lint
6
+ from io import StringIO
7
+ import sys
8
+ import torch
9
+ from huggingface_hub import hf_hub_url, cached_download, HfApi
10
+
11
+ # Set your Hugging Face API key here
12
+ hf_token = "YOUR_HUGGING_FACE_API_KEY" # Replace with your actual token
13
+
14
+ HUGGING_FACE_REPO_URL = "https://huggingface.co/spaces/acecalisto3/DevToolKit"
15
+ PROJECT_ROOT = "projects"
16
+ AGENT_DIRECTORY = "agents"
17
+
18
+ # Global state to manage communication between Tool Box and Workspace Chat App
19
+ if 'chat_history' not in st.session_state:
20
+ st.session_state.chat_history = []
21
+ if 'terminal_history' not in st.session_state:
22
+ st.session_state.terminal_history = []
23
+ if 'workspace_projects' not in st.session_state:
24
+ st.session_state.workspace_projects = {}
25
+ if 'available_agents' not in st.session_state:
26
+ st.session_state.available_agents = []
27
+ if 'current_state' not in st.session_state:
28
+ st.session_state.current_state = {
29
+ 'toolbox': {},
30
+ 'workspace_chat': {}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  }
32
 
33
+ # List of top downloaded free code-generative models from Hugging Face Hub
34
+ AVAILABLE_CODE_GENERATIVE_MODELS = [
35
+ "bigcode/starcoder", # Popular and powerful
36
+ "Salesforce/codegen-350M-mono", # Smaller, good for quick tasks
37
+ "microsoft/CodeGPT-small", # Smaller, good for quick tasks
38
+ "google/flan-t5-xl", # Powerful, good for complex tasks
39
+ "facebook/bart-large-cnn", # Good for text-to-code tasks
40
+ ]
41
+
42
+ # Load pre-trained RAG retriever
43
+ rag_retriever = RagRetriever.from_pretrained("facebook/rag-token-base") # Use a Hugging Face RAG model
44
+
45
+ # Load pre-trained chat model
46
+ chat_model = AutoModelForSeq2SeqLM.from_pretrained("microsoft/DialoGPT-medium") # Use a Hugging Face chat model
47
+
48
+ # Load tokenizer
49
+ tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
50
+
51
+ def process_input(user_input):
52
+ # Input pipeline: Tokenize and preprocess user input
53
+ input_ids = tokenizer(user_input, return_tensors="pt").input_ids
54
+ attention_mask = tokenizer(user_input, return_tensors="pt").attention_mask
55
+
56
+ # RAG model: Generate response
57
+ with torch.no_grad():
58
+ output = rag_retriever(input_ids, attention_mask=attention_mask)
59
+ response = output.generator_outputs[0].sequences[0]
60
+
61
+ # Chat model: Refine response
62
+ chat_input = tokenizer(response, return_tensors="pt")
63
+ chat_input["input_ids"] = chat_input["input_ids"].unsqueeze(0)
64
+ chat_input["attention_mask"] = chat_input["attention_mask"].unsqueeze(0)
65
+ with torch.no_grad():
66
+ chat_output = chat_model(**chat_input)
67
+ refined_response = chat_output.sequences[0]
68
+
69
+ # Output pipeline: Return final response
70
+ return refined_response
71
+
72
+ class AIAgent:
73
+ def __init__(self, name, description, skills):
74
+ self.name = name
75
+ self.description = description
76
+ self.skills = skills
77
+
78
+ def create_agent_prompt(self):
79
+ skills_str = '\n'.join([f"* {skill}" for skill in self.skills])
80
+ agent_prompt = f"""
81
+ As an elite expert developer, my name is {self.name}. I possess a comprehensive understanding of the following areas:
82
+ {skills_str}
83
+ 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.
84
+ """
85
+ return agent_prompt
86
+
87
+ def autonomous_build(self, chat_history, workspace_projects, project_name, selected_model):
88
+ """
89
+ Autonomous build logic that continues based on the state of chat history and workspace projects.
90
+ """
91
+ summary = "Chat History:\n" + "\n".join([f"User: {u}\nAgent: {a}" for u, a in chat_history])
92
+ summary += "\n\nWorkspace Projects:\n" + "\n".join([f"{p}: {details}" for p, details in workspace_projects.items()])
93
+
94
+ # Analyze chat history and workspace projects to suggest actions
95
+ # Example:
96
+ # - Check if the user has requested to create a new file
97
+ # - Check if the user has requested to install a package
98
+ # - Check if the user has requested to run a command
99
+ # - Check if the user has requested to generate code
100
+ # - Check if the user has requested to translate code
101
+ # - Check if the user has requested to summarize text
102
+ # - Check if the user has requested to analyze sentiment
103
+
104
+ # Generate a response based on the analysis
105
+ next_step = "Based on the current state, the next logical step is to implement the main application logic."
106
+
107
+ # Ensure project folder exists
108
+ project_path = os.path.join(PROJECT_ROOT, project_name)
109
+ if not os.path.exists(project_path):
110
+ os.makedirs(project_path)
111
+
112
+ # Create requirements.txt if it doesn't exist
113
+ requirements_file = os.path.join(project_path, "requirements.txt")
114
+ if not os.path.exists(requirements_file):
115
+ with open(requirements_file, "w") as f:
116
+ f.write("# Add your project's dependencies here\n")
117
+
118
+ # Create app.py if it doesn't exist
119
+ app_file = os.path.join(project_path, "app.py")
120
+ if not os.path.exists(app_file):
121
+ with open(app_file, "w") as f:
122
+ f.write("# Your project's main application logic goes here\n")
123
+
124
+ # Generate GUI code for app.py if requested
125
+ if "create a gui" in summary.lower():
126
+ gui_code = generate_code("Create a simple GUI for this application", selected_model)
127
+ with open(app_file, "a") as f:
128
+ f.write(gui_code)
129
+
130
+ # Run the default build process
131
+ build_command = "pip install -r requirements.txt && python app.py"
132
+ try:
133
+ result = subprocess.run(build_command, shell=True, capture_output=True, text=True, cwd=project_path)
134
+ st.write(f"Build Output:\n{result.stdout}")
135
+ if result.stderr:
136
+ st.error(f"Build Errors:\n{result.stderr}")
137
+ except Exception as e:
138
+ st.error(f"Build Error: {e}")
139
+
140
+ return summary, next_step
141
+
142
+ def save_agent_to_file(agent):
143
+ """Saves the agent's prompt to a file."""
144
+ if not os.path.exists(AGENT_DIRECTORY):
145
+ os.makedirs(AGENT_DIRECTORY)
146
+ file_path = os.path.join(AGENT_DIRECTORY, f"{agent.name}.txt")
147
+ with open(file_path, "w") as file:
148
+ file.write(agent.create_agent_prompt())
149
+ st.session_state.available_agents.append(agent.name)
150
+
151
+ def load_agent_prompt(agent_name):
152
+ """Loads an agent prompt from a file."""
153
+ file_path = os.path.join(AGENT_DIRECTORY, f"{agent_name}.txt")
154
+ if os.path.exists(file_path):
155
+ with open(file_path, "r") as file:
156
+ agent_prompt = file.read()
157
+ return agent_prompt
158
  else:
159
+ return None
160
+
161
+ def create_agent_from_text(name, text):
162
+ skills = text.split('\n')
163
+ agent = AIAgent(name, "AI agent created from text input.", skills)
164
+ save_agent_to_file(agent)
165
+ return agent.create_agent_prompt()
166
+
167
+ def chat_interface_with_agent(input_text, agent_name):
168
+ agent_prompt = load_agent_prompt(agent_name)
169
+ if agent_prompt is None:
170
+ return f"Agent {agent_name} not found."
171
+
172
+ model_name ="MaziyarPanahi/Codestral-22B-v0.1-GGUF"
173
+ try:
174
+ from transformers import AutoModel, AutoTokenizer # Import AutoModel here
175
+ model = AutoModel.from_pretrained("MaziyarPanahi/Codestral-22B-v0.1-GGUF")
176
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
177
+ generator = pipeline("text-generation", model=model, tokenizer=tokenizer)
178
+ except EnvironmentError as e:
179
+ return f"Error loading model: {e}"
180
+
181
+ combined_input = f"{agent_prompt}\n\nUser: {input_text}\nAgent:"
182
 
183
+ input_ids = tokenizer.encode(combined_input, return_tensors="pt")
184
+ max_input_length = 900
185
+ if input_ids.shape[1] > max_input_length:
186
+ input_ids = input_ids[:, :max_input_length]
187
+
188
+ outputs = model.generate(
189
+ input_ids, max_new_tokens=50, num_return_sequences=1, do_sample=True,
190
+ pad_token_id=tokenizer.eos_token_id # Set pad_token_id to eos_token_id
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
191
  )
192
+ response = tokenizer.decode(outputs[0], skip_special_tokens=True)
193
+ return response
194
+
195
+ # Terminal interface
196
+ def terminal_interface(command, project_name=None):
197
+ if project_name:
198
+ project_path = os.path.join(PROJECT_ROOT, project_name)
199
+ if not os.path.exists(project_path):
200
+ return f"Project {project_name} does not exist."
201
+ result = subprocess.run(command, shell=True, capture_output=True, text=True, cwd=project_path)
202
+ else:
203
+ result = subprocess.run(command, shell=True, capture_output=True, text=True)
204
+ return result.stdout
205
+
206
+ # Code editor interface
207
+ def code_editor_interface(code):
208
+ try:
209
+ formatted_code = black.format_str(code, mode=black.FileMode())
210
+ except black.NothingChanged:
211
+ formatted_code = code
212
+
213
+ result = StringIO()
214
+ sys.stdout = result
215
+ sys.stderr = result
216
+
217
+ (pylint_stdout, pylint_stderr) = lint.py_run(code, return_std=True)
218
+ sys.stdout = sys.__stdout__
219
+ sys.stderr = sys.__stderr__
220
+
221
+ lint_message = pylint_stdout.getvalue() + pylint_stderr.getvalue()
222
+
223
+ return formatted_code, lint_message
224
+
225
+ # Text summarization tool
226
+ def summarize_text(text):
227
+ summarizer = pipeline("summarization")
228
+ summary = summarizer(text, max_length=130, min_length=30, do_sample=False)
229
+ return summary[0]['summary_text']
230
+
231
+ # Sentiment analysis tool
232
+ def sentiment_analysis(text):
233
+ analyzer = pipeline("sentiment-analysis")
234
+ result = analyzer(text)
235
+ return result[0]['label']
236
+
237
+ # Text translation tool (code translation)
238
+ def translate_code(code, source_language, target_language):
239
+ # Use a Hugging Face translation model instead of OpenAI
240
+ translator = pipeline("translation", model="bartowski/Codestral-22B-v0.1-GGUF") # Example: English to Spanish
241
+ translated_code = translator(code, target_lang=target_language)[0]['translation_text']
242
+ return translated_code
243
+
244
+ def generate_code(code_idea, model_name):
245
+ """Generates code using the selected model."""
246
+ try:
247
+ generator = pipeline('text-generation', model=model_name)
248
+ generated_code = generator(code_idea, max_length=1000, num_return_sequences=1)[0]['generated_text']
249
+ return generated_code
250
+ except Exception as e:
251
+ return f"Error generating code: {e}"
252
+
253
+ def chat_interface(input_text):
254
+ """Handles general chat interactions with the user."""
255
+ # Use a Hugging Face chatbot model or your own logic
256
+ chatbot = pipeline("text-generation", model="microsoft/DialoGPT-medium")
257
+ response = chatbot(input_text, max_length=50, num_return_sequences=1)[0]['generated_text']
258
+ return response
259
+
260
+ # Workspace interface
261
+ def workspace_interface(project_name):
262
+ project_path = os.path.join(PROJECT_ROOT, project_name)
263
+ if not os.path.exists(project_path):
264
+ os.makedirs(project_path)
265
+ st.session_state.workspace_projects[project_name] = {'files': []}
266
+ return f"Project '{project_name}' created successfully."
267
+ else:
268
+ return f"Project '{project_name}' already exists."
269
 
270
+ # Add code to workspace
271
+ def add_code_to_workspace(project_name, code, file_name):
272
+ project_path = os.path.join(PROJECT_ROOT, project_name)
273
+ if not os.path.exists(project_path):
274
+ return f"Project '{project_name}' does not exist."
275
+
276
+ file_path = os.path.join(project_path, file_name)
277
+ with open(file_path, "w") as file:
278
+ file.write(code)
279
+ st.session_state.workspace_projects[project_name]['files'].append(file_name)
280
+ return f"Code added to '{file_name}' in project '{project_name}'."
281
+
282
+ # Streamlit App
283
+ st.title("AI Agent Creator")
284
+
285
+ # Sidebar navigation
286
+ st.sidebar.title("Navigation")
287
+ app_mode = st.sidebar.selectbox("Choose the app mode", ["AI Agent Creator", "Tool Box", "Workspace Chat App"])
288
+
289
+ # Get Hugging Face token from secrets.toml
290
+ hf_token = st.secrets["huggingface"]["hf_token"]
291
+
292
+ if app_mode == "AI Agent Creator":
293
+ # AI Agent Creator
294
+ st.header("Create an AI Agent from Text")
295
+
296
+ st.subheader("From Text")
297
+ agent_name = st.text_input("Enter agent name:")
298
+ text_input = st.text_area("Enter skills (one per line):")
299
+ if st.button("Create Agent"):
300
+ agent_prompt = create_agent_from_text(agent_name, text_input)
301
+ st.success(f"Agent '{agent_name}' created and saved successfully.")
302
+ st.session_state.available_agents.append(agent_name)
303
+
304
+ elif app_mode == "Tool Box":
305
+ # Tool Box
306
+ st.header("AI-Powered Tools")
307
+
308
+ # Chat Interface
309
+ st.subheader("Chat with CodeCraft")
310
+ chat_input = st.text_area("Enter your message:")
311
+ if st.button("Send"):
312
+ chat_response = chat_interface(chat_input)
313
+ st.session_state.chat_history.append((chat_input, chat_response))
314
+ st.write(f"CodeCraft: {chat_response}")
315
+
316
+ # Terminal Interface
317
+ st.subheader("Terminal")
318
+ terminal_input = st.text_input("Enter a command:")
319
+ if st.button("Run"):
320
+ terminal_output = terminal_interface(terminal_input)
321
+ st.session_state.terminal_history.append((terminal_input, terminal_output))
322
+ st.code(terminal_output, language="bash")
323
+
324
+ # Code Editor Interface
325
+ st.subheader("Code Editor")
326
+ code_editor = st.text_area("Write your code:", height=300)
327
+ if st.button("Format & Lint"):
328
+ formatted_code, lint_message = code_editor_interface(code_editor)
329
+ st.code(formatted_code, language="python")
330
+ st.info(lint_message)
331
+
332
+ # Text Summarization Tool
333
+ st.subheader("Summarize Text")
334
+ text_to_summarize = st.text_area("Enter text to summarize:")
335
+ if st.button("Summarize"):
336
+ summary = summarize_text(text_to_summarize)
337
+ st.write(f"Summary: {summary}")
338
+
339
+ # Sentiment Analysis Tool
340
+ st.subheader("Sentiment Analysis")
341
+ sentiment_text = st.text_area("Enter text for sentiment analysis:")
342
+ if st.button("Analyze Sentiment"):
343
+ sentiment = sentiment_analysis(sentiment_text)
344
+ st.write(f"Sentiment: {sentiment}")
345
+
346
+ # Text Translation Tool (Code Translation)
347
+ st.subheader("Translate Code")
348
+ code_to_translate = st.text_area("Enter code to translate:")
349
+ source_language = st.text_input("Enter source language (e.g., 'Python'):")
350
+ target_language = st.text_input("Enter target language (e.g., 'JavaScript'):")
351
+ if st.button("Translate Code"):
352
+ translated_code = translate_code(code_to_translate, source_language, target_language)
353
+ st.code(translated_code, language=target_language.lower())
354
+
355
+ # Code Generation
356
+ st.subheader("Code Generation")
357
+ code_idea = st.text_input("Enter your code idea:")
358
+ if st.button("Generate Code"):
359
+ generated_code = generate_code(code_idea)
360
+ st.code(generated_code, language="python")
361
+
362
+ elif app_mode == "Workspace Chat App":
363
+ # Workspace Chat App
364
+ st.header("Workspace Chat App")
365
+
366
+ # Project Workspace Creation
367
+ st.subheader("Create a New Project")
368
+ project_name = st.text_input("Enter project name:")
369
+ if st.button("Create Project"):
370
+ workspace_status = workspace_interface(project_name)
371
+ st.success(workspace_status)
372
+
373
+ # Automatically create requirements.txt and app.py
374
+ project_path = os.path.join(PROJECT_ROOT, project_name)
375
+ requirements_file = os.path.join(project_path, "requirements.txt")
376
+ if not os.path.exists(requirements_file):
377
+ with open(requirements_file, "w") as f:
378
+ f.write("# Add your project's dependencies here\n")
379
+
380
+ app_file = os.path.join(project_path, "app.py")
381
+ if not os.path.exists(app_file):
382
+ with open(app_file, "w") as f:
383
+ f.write("# Your project's main application logic goes here\n")
384
+
385
+ # Add Code to Workspace
386
+ st.subheader("Add Code to Workspace")
387
+ code_to_add = st.text_area("Enter code to add to workspace:")
388
+ file_name = st.text_input("Enter file name (e.g., 'app.py'):")
389
+ if st.button("Add Code"):
390
+ add_code_status = add_code_to_workspace(project_name, code_to_add, file_name)
391
+ st.session_state.terminal_history.append((f"Add Code: {code_to_add}", add_code_status))
392
+ st.success(add_code_status)
393
+
394
+ # Terminal Interface with Project Context
395
+ st.subheader("Terminal (Workspace Context)")
396
+ terminal_input = st.text_input("Enter a command within the workspace:")
397
+ if st.button("Run Command"):
398
+ terminal_output = terminal_interface(terminal_input, project_name)
399
+ st.session_state.terminal_history.append((terminal_input, terminal_output))
400
+ st.code(terminal_output, language="bash")
401
+
402
+ # Chat Interface for Guidance
403
+ st.subheader("Chat with CodeCraft for Guidance")
404
+ chat_input = st.text_area("Enter your message for guidance:")
405
+ if st.button("Get Guidance"):
406
+ chat_response = chat_interface(chat_input)
407
+ st.session_state.chat_history.append((chat_input, chat_response))
408
+ st.write(f"CodeCraft: {chat_response}")
409
+
410
+ # Display Chat History
411
+ st.subheader("Chat History")
412
+ for user_input, response in st.session_state.chat_history:
413
+ st.write(f"User: {user_input}")
414
+ st.write(f"CodeCraft: {response}")
415
+
416
+ # Display Terminal History
417
+ st.subheader("Terminal History")
418
+ for command, output in st.session_state.terminal_history:
419
+ st.write(f"Command: {command}")
420
+ st.code(output, language="bash")
421
+
422
+ # Display Projects and Files
423
+ st.subheader("Workspace Projects")
424
+ for project, details in st.session_state.workspace_projects.items():
425
+ st.write(f"Project: {project}")
426
+ for file in details['files']:
427
+ st.write(f" - {file}")
428
+
429
+ # Chat with AI Agents
430
+ st.subheader("Chat with AI Agents")
431
+ selected_agent = st.selectbox("Select an AI agent", st.session_state.available_agents)
432
+ agent_chat_input = st.text_area("Enter your message for the agent:")
433
+ if st.button("Send to Agent"):
434
+ agent_chat_response = chat_interface_with_agent(agent_chat_input, selected_agent)
435
+ st.session_state.chat_history.append((agent_chat_input, agent_chat_response))
436
+ st.write(f"{selected_agent}: {agent_chat_response}")
437
+
438
+ # Code Generation
439
+ st.subheader("Code Generation")
440
+ code_idea = st.text_input("Enter your code idea:")
441
+
442
+ # Model Selection Menu
443
+ selected_model = st.selectbox("Select a code-generative model", AVAILABLE_CODE_GENERATIVE_MODELS)
444
+
445
+ if st.button("Generate Code"):
446
+ generated_code = generate_code(code_idea, selected_model)
447
+ st.code(generated_code, language="python")
448
+
449
+ # Automate Build Process
450
+ st.subheader("Automate Build Process")
451
+ if st.button("Automate"):
452
+ agent = AIAgent(selected_agent, "", []) # Load the agent without skills for now
453
+ summary, next_step = agent.autonomous_build(st.session_state.chat_history, st.session_state.workspace_projects, project_name, selected_model)
454
+ st.write("Autonomous Build Summary:")
455
+ st.write(summary)
456
+ st.write("Next Step:")
457
+ st.write(next_step)
458
+
459
+ # Use the hf_token to interact with the Hugging Face API
460
+ api = HfApi(token=hf_token)
461
+ # Function to create a Space on Hugging Face
462
+ def create_space(api, name, description, public, files, entrypoint="launch.py"):
463
+ url = f"{hf_hub_url()}spaces/{name}/prepare-repo"
464
+ headers = {"Authorization": f"Bearer {api.access_token}"}````
465
+
466
+ i need to integrate logic from these below, into my existing app.py above:
467
+ ````import os
468
+ import subprocess
469
+ import streamlit as st
470
+ from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer, AutoModel, RagRetriever, AutoModelForSeq2SeqLM
471
+ import black
472
+ from pylint import lint
473
+ from io import StringIO
474
+ import sys
475
+ import torch
476
+ from huggingface_hub import hf_hub_url, cached_download, HfApi
477
+
478
+ # Set your Hugging Face API key here
479
+ hf_token = "YOUR_HUGGING_FACE_API_KEY" # Replace with your actual token
480
+
481
+ # Other code remains unchanged
482
+
483
+ class AIAgent:
484
+ def __init__(self, name, description, skills, hf_api=None):
485
+ self.name = name
486
+ self.description = description
487
+ self.skills = skills
488
+ self._hf_api = hf_api
489
+
490
+ @property
491
+ def hf_api(self):
492
+ if not self._hf_api and self.has_valid_hf_token():
493
+ self._hf_api = HfApi(token=self._hf_token)
494
+ return self._hf_api
495
+
496
+ def has_valid_hf_token(self):
497
+ return bool(self._hf_token)
498
+
499
+ async def autonomous_build(self, chat_history, workspace_projects, project_name, selected_model, hf_token):
500
+ self._hf_token = hf_token
501
+ # Continuation of previous methods
502
+
503
+ def deploy_built_space_to_hf(self):
504
+ if not self._hf_api or not self._hf_token:
505
+ raise ValueError("Cannot deploy the Space since no valid Hugoging Face API connection was established.")
506
+
507
+ repository_name = f"my-awesome-space_{datetime.now().timestamp()}"
508
+ files = get_built_space_files()
509
+
510
+ commit_response = self.hf_api.commit_repo(
511
+ repo_id=repository_name,
512
+ branch="main",
513
+ commits=[{"message": "Built Space Commit", "tree": tree_payload}]
514
+ )
515
+
516
+ print("Commit successful:", commit_response)
517
+ self.publish_space(repository_name)
518
+
519
+ def publish_space(self, repository_name):
520
+ publishing_response = self.hf_api.create_model_version(
521
+ model_name=repository_name,
522
+ repo_id=repository_name,
523
+ model_card={},
524
+ library_card={}
525
+ )
526
+
527
+ print("Space published:", publishing_response)
528
+
529
+ def create_space(api, name, description, public, files, entrypoint="launch.py"):
530
+ url = f"{hf_hub_url()}spaces/{name}/prepare-repo"
531
+ headers = {"Authorization": f"Bearer {api.access_token}"}
532
+
533
+ payload = {
534
+ "public": public,
535
+ "gitignore_template": "web",
536
+ "default_branch": "main",
537
+ "archived": False,
538
+ "files": []
539
+ }
540
 
541
+ for filename, contents in files.items():
542
+ data = {
543
+ "content": contents,
544
+ "path": filename,
545
+ "encoding": "utf-8",
546
+ "mode": "overwrite" if "#{random.randint(0, 1)}" not in contents else "merge",
547
+ }
548
+ payload["files"].append(data)
549
+
550
+ response = requests.post(url, json=payload, headers=headers)
551
+ response.raise_for_status()
552
+ location = response.headers.get("Location")
553
+ wait_for_processing(location, api)
554
+
555
+ return Repository(name=name, api=api)