Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,317 +1,284 @@
|
|
1 |
-
import
|
2 |
-
import
|
3 |
-
import
|
4 |
-
|
5 |
-
import
|
6 |
-
|
7 |
-
from
|
8 |
-
|
9 |
-
import
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
"""Saves the agent's prompt to a file."""
|
58 |
-
if not os.path.exists(AGENT_DIRECTORY):
|
59 |
-
os.makedirs(AGENT_DIRECTORY)
|
60 |
-
file_path = os.path.join(AGENT_DIRECTORY, f"{agent.name}.txt")
|
61 |
-
with open(file_path, "w") as file:
|
62 |
-
file.write(agent.create_agent_prompt())
|
63 |
-
st.session_state.available_agents.append(agent.name)
|
64 |
-
|
65 |
-
def load_agent_prompt(agent_name):
|
66 |
-
"""Loads an agent prompt from a file."""
|
67 |
-
file_path = os.path.join(AGENT_DIRECTORY, f"{agent_name}.txt")
|
68 |
-
if os.path.exists(file_path):
|
69 |
-
with open(file_path, "r") as file:
|
70 |
-
agent_prompt = file.read()
|
71 |
-
return agent_prompt
|
72 |
else:
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
save_agent_to_file(agent)
|
79 |
-
return agent.create_agent_prompt()
|
80 |
-
|
81 |
-
# Chat interface using a selected agent (Hugging Face version)
|
82 |
-
def chat_interface_with_agent(input_text, agent_name):
|
83 |
-
agent_prompt = load_agent_prompt(agent_name)
|
84 |
-
if agent_prompt is None:
|
85 |
-
return f"Agent {agent_name} not found."
|
86 |
-
|
87 |
-
# Use a Hugging Face model for chat with the agent
|
88 |
-
# Example: Using the 'google/flan-t5-xl' model
|
89 |
-
generator = pipeline("text-generation", model="google/flan-t5-xl", use_auth_token=HF_API_KEY)
|
90 |
-
|
91 |
-
# Combine the agent prompt with user input
|
92 |
-
combined_input = f"{agent_prompt}\n\nUser: {input_text}\nAgent:"
|
93 |
-
|
94 |
-
# Generate chatbot response
|
95 |
-
response = generator(combined_input, max_length=150, num_return_sequences=1, do_sample=True)
|
96 |
-
return response[0]['generated_text']
|
97 |
-
|
98 |
-
# Terminal interface
|
99 |
-
def terminal_interface(command, project_name=None):
|
100 |
-
if project_name:
|
101 |
-
project_path = os.path.join(PROJECT_ROOT, project_name)
|
102 |
-
result = subprocess.run(command, shell=True, capture_output=True, text=True, cwd=project_path)
|
103 |
else:
|
104 |
-
|
105 |
-
return
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
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 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
253 |
-
|
254 |
-
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
#
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
-
|
286 |
-
|
287 |
-
|
288 |
-
|
289 |
-
|
290 |
-
|
291 |
-
|
292 |
-
|
293 |
-
#
|
294 |
-
|
295 |
-
|
296 |
-
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
-
#
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
#
|
310 |
-
|
311 |
-
|
312 |
-
|
313 |
-
|
314 |
-
|
315 |
-
|
316 |
-
|
317 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
import plotly.graph_objects as go
|
24 |
+
from collections import Counter
|
25 |
+
import warnings
|
26 |
+
warnings.filterwarnings("ignore")
|
27 |
+
|
28 |
+
# Set up logging
|
29 |
+
import logging
|
30 |
+
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
31 |
+
|
32 |
+
# Function to fetch HTML content from GitHub issue pages
|
33 |
+
def fetch_issue_data(username, repository, start_page, end_page):
|
34 |
+
issues_data = []
|
35 |
+
for page in range(start_page, end_page + 1):
|
36 |
+
url = f"https://github.com/{username}/{repository}/issues?page={page}"
|
37 |
+
response = requests.get(url)
|
38 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
39 |
+
issue_elements = soup.find_all('div', class_='flex-shrink-0')
|
40 |
+
for issue_element in issue_elements:
|
41 |
+
issue_link = issue_element.find('a', class_='Link--primary')['href']
|
42 |
+
issue_url = f"https://github.com{issue_link}"
|
43 |
+
issue_data = fetch_issue_details(issue_url)
|
44 |
+
issues_data.append(issue_data)
|
45 |
+
return issues_data
|
46 |
+
|
47 |
+
# Function to fetch details of a specific issue
|
48 |
+
def fetch_issue_details(issue_url):
|
49 |
+
response = requests.get(issue_url)
|
50 |
+
soup = BeautifulSoup(response.content, 'html.parser')
|
51 |
+
issue_title = soup.find('h1', class_='gh-header-title').text.strip()
|
52 |
+
issue_body = soup.find('div', class_='markdown-body').text.strip()
|
53 |
+
issue_created_at = soup.find('relative-time')['datetime']
|
54 |
+
issue_closed_at = soup.find('relative-time', class_='no-wrap')
|
55 |
+
if issue_closed_at:
|
56 |
+
issue_closed_at = issue_closed_at['datetime']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
else:
|
58 |
+
issue_closed_at = None
|
59 |
+
issue_author = soup.find('a', class_='author').text.strip()
|
60 |
+
issue_assignee = soup.find('a', class_='Link--muted')
|
61 |
+
if issue_assignee:
|
62 |
+
issue_assignee = issue_assignee.text.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
else:
|
64 |
+
issue_assignee = None
|
65 |
+
return {
|
66 |
+
'title': issue_title,
|
67 |
+
'body': issue_body,
|
68 |
+
'created_at': issue_created_at,
|
69 |
+
'closed_at': issue_closed_at,
|
70 |
+
'author': issue_author,
|
71 |
+
'assignee': issue_assignee
|
72 |
+
}
|
73 |
+
|
74 |
+
# Function to clean and structure the data
|
75 |
+
def clean_and_structure_data(issues_data):
|
76 |
+
df = pd.DataFrame(issues_data)
|
77 |
+
df['created_at'] = pd.to_datetime(df['created_at'])
|
78 |
+
df['closed_at'] = pd.to_datetime(df['closed_at'])
|
79 |
+
df['resolution_time'] = (df['closed_at'] - df['created_at']).dt.days
|
80 |
+
df['resolution_time'] = df['resolution_time'].fillna(-1)
|
81 |
+
df['is_closed'] = (df['closed_at'].notna()).astype(int)
|
82 |
+
return df
|
83 |
+
|
84 |
+
# Function for exploratory data analysis (EDA)
|
85 |
+
def perform_eda(df):
|
86 |
+
# Descriptive statistics
|
87 |
+
print("Descriptive Statistics:")
|
88 |
+
print(df.describe())
|
89 |
+
|
90 |
+
# Visualizations
|
91 |
+
plt.figure(figsize=(10, 6))
|
92 |
+
sns.histplot(df['resolution_time'], kde=True)
|
93 |
+
plt.title('Distribution of Issue Resolution Time')
|
94 |
+
plt.xlabel('Resolution Time (Days)')
|
95 |
+
plt.ylabel('Frequency')
|
96 |
+
plt.show()
|
97 |
+
|
98 |
+
# Trend analysis
|
99 |
+
df['created_at_month'] = df['created_at'].dt.month
|
100 |
+
plt.figure(figsize=(10, 6))
|
101 |
+
sns.lineplot(x='created_at_month', y='resolution_time', data=df)
|
102 |
+
plt.title('Trend of Issue Resolution Time Over Months')
|
103 |
+
plt.xlabel('Month')
|
104 |
+
plt.ylabel('Resolution Time (Days)')
|
105 |
+
plt.show()
|
106 |
+
|
107 |
+
# Top Authors and Assignees
|
108 |
+
top_authors = df['author'].value_counts().nlargest(10)
|
109 |
+
top_assignees = df['assignee'].value_counts().nlargest(10)
|
110 |
+
print("\nTop 10 Authors:")
|
111 |
+
print(top_authors)
|
112 |
+
print("\nTop 10 Assignees:")
|
113 |
+
print(top_assignees)
|
114 |
+
|
115 |
+
# Function for text analysis using NLP
|
116 |
+
def analyze_text_content(df):
|
117 |
+
# Text preprocessing
|
118 |
+
stop_words = set(stopwords.words('english'))
|
119 |
+
lemmatizer = WordNetLemmatizer()
|
120 |
+
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]))
|
121 |
+
|
122 |
+
# Topic modeling
|
123 |
+
dictionary = Dictionary([word_tokenize(text) for text in df['processed_body']])
|
124 |
+
corpus = [dictionary.doc2bow(word_tokenize(text)) for text in df['processed_body']]
|
125 |
+
lda_model = LdaModel(corpus, num_topics=5, id2word=dictionary)
|
126 |
+
print("Top 5 Topics:")
|
127 |
+
for topic in lda_model.print_topics(num_words=5):
|
128 |
+
print(topic)
|
129 |
+
|
130 |
+
# Sentiment analysis
|
131 |
+
analyzer = SentimentIntensityAnalyzer()
|
132 |
+
df['sentiment'] = df['body'].apply(lambda text: analyzer.polarity_scores(text)['compound'])
|
133 |
+
print("Sentiment Analysis:")
|
134 |
+
print(df['sentiment'].describe())
|
135 |
+
|
136 |
+
# Word Cloud for Common Words
|
137 |
+
from wordcloud import WordCloud
|
138 |
+
all_words = ' '.join([text for text in df['processed_body']])
|
139 |
+
wordcloud = WordCloud(width=800, height=400, background_color='white').generate(all_words)
|
140 |
+
plt.figure(figsize=(10, 6), facecolor=None)
|
141 |
+
plt.imshow(wordcloud)
|
142 |
+
plt.axis("off")
|
143 |
+
plt.tight_layout(pad=0)
|
144 |
+
plt.show()
|
145 |
+
|
146 |
+
# Function to create a network graph of issues, authors, and assignees
|
147 |
+
def create_network_graph(df):
|
148 |
+
graph = nx.Graph()
|
149 |
+
for index, row in df.iterrows():
|
150 |
+
graph.add_node(row['title'], type='issue')
|
151 |
+
graph.add_node(row['author'], type='author')
|
152 |
+
if row['assignee']:
|
153 |
+
graph.add_node(row['assignee'], type='assignee')
|
154 |
+
graph.add_edge(row['title'], row['author'])
|
155 |
+
if row['assignee']:
|
156 |
+
graph.add_edge(row['title'], row['assignee'])
|
157 |
+
|
158 |
+
# Interactive Network Graph with Plotly
|
159 |
+
pos = nx.spring_layout(graph, k=0.5)
|
160 |
+
edge_x = []
|
161 |
+
edge_y = []
|
162 |
+
for edge in graph.edges():
|
163 |
+
x0, y0 = pos[edge[0]]
|
164 |
+
x1, y1 = pos[edge[1]]
|
165 |
+
edge_x.append([x0, x1, None])
|
166 |
+
edge_y.append([y0, y1, None])
|
167 |
+
|
168 |
+
edge_trace = go.Scatter(
|
169 |
+
x=edge_x,
|
170 |
+
y=edge_y,
|
171 |
+
line=dict(width=0.5, color='#888'),
|
172 |
+
hoverinfo='none',
|
173 |
+
mode='lines'
|
174 |
+
)
|
175 |
+
|
176 |
+
node_x = []
|
177 |
+
node_y = []
|
178 |
+
for node in graph.nodes():
|
179 |
+
x, y = pos[node]
|
180 |
+
node_x.append(x)
|
181 |
+
node_y.append(y)
|
182 |
+
|
183 |
+
node_trace = go.Scatter(
|
184 |
+
x=node_x,
|
185 |
+
y=node_y,
|
186 |
+
mode='markers',
|
187 |
+
marker=dict(
|
188 |
+
color=[],
|
189 |
+
size=10,
|
190 |
+
line=dict(width=2, color='black')
|
191 |
+
),
|
192 |
+
text=[],
|
193 |
+
hoverinfo='text'
|
194 |
+
)
|
195 |
+
|
196 |
+
# Set node colors based on type
|
197 |
+
node_colors = []
|
198 |
+
for node in graph.nodes():
|
199 |
+
if graph.nodes[node]['type'] == 'issue':
|
200 |
+
node_colors.append('red')
|
201 |
+
elif graph.nodes[node]['type'] == 'author':
|
202 |
+
node_colors.append('blue')
|
203 |
+
else:
|
204 |
+
node_colors.append('green')
|
205 |
+
|
206 |
+
# Set node labels
|
207 |
+
node_labels = []
|
208 |
+
for node in graph.nodes():
|
209 |
+
node_labels.append(node)
|
210 |
+
|
211 |
+
node_trace.marker.color = node_colors
|
212 |
+
node_trace.text = node_labels
|
213 |
+
|
214 |
+
# Create the figure
|
215 |
+
fig = go.Figure(data=[edge_trace, node_trace],
|
216 |
+
layout=go.Layout(
|
217 |
+
title="GitHub Issue Network Graph",
|
218 |
+
showlegend=False,
|
219 |
+
hovermode='closest',
|
220 |
+
margin=dict(b=20, l=5, r=5, t=40),
|
221 |
+
xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
|
222 |
+
yaxis=dict(showgrid=False, zeroline=False, showticklabels=False)
|
223 |
+
)
|
224 |
+
)
|
225 |
+
|
226 |
+
fig.show()
|
227 |
+
|
228 |
+
# Function to build a predictive model for issue resolution time
|
229 |
+
def build_predictive_model(df):
|
230 |
+
# Feature engineering
|
231 |
+
df['created_at_day'] = df['created_at'].dt.day
|
232 |
+
df['created_at_weekday'] = df['created_at'].dt.weekday
|
233 |
+
df['created_at_hour'] = df['created_at'].dt.hour
|
234 |
+
df['author_encoded'] = df['author'].astype('category').cat.codes
|
235 |
+
df['assignee_encoded'] = df['assignee'].astype('category').cat.codes
|
236 |
+
|
237 |
+
# Select features and target variable
|
238 |
+
features = ['created_at_day', 'created_at_weekday', 'created_at_hour', 'author_encoded', 'assignee_encoded', 'sentiment']
|
239 |
+
target = 'resolution_time'
|
240 |
+
|
241 |
+
# Split data into training and testing sets
|
242 |
+
X_train, X_test, y_train, y_test = train_test_split(df[features], df[target], test_size=0.2, random_state=42)
|
243 |
+
|
244 |
+
# Create a pipeline for feature scaling and model training
|
245 |
+
pipeline = Pipeline([
|
246 |
+
('scaler', StandardScaler()),
|
247 |
+
('model', RandomForestClassifier(random_state=42))
|
248 |
+
])
|
249 |
+
|
250 |
+
# Train the model
|
251 |
+
pipeline.fit(X_train, y_train)
|
252 |
+
|
253 |
+
# Evaluate the model
|
254 |
+
y_pred = pipeline.predict(X_test)
|
255 |
+
accuracy = accuracy_score(y_test, y_pred)
|
256 |
+
print("Accuracy:", accuracy)
|
257 |
+
print(classification_report(y_test, y_pred))
|
258 |
+
|
259 |
+
# Make predictions on new data
|
260 |
+
# ...
|
261 |
+
|
262 |
+
# Main function
|
263 |
+
if __name__ == "__main__":
|
264 |
+
# Replace with your GitHub username and repository name
|
265 |
+
username = "miagiii"
|
266 |
+
repository = "miagiii"
|
267 |
+
|
268 |
+
# Fetch issue data from GitHub
|
269 |
+
issues_data = fetch_issue_data(username, repository, 1, 10) # Fetch issues from pages 1 to 10
|
270 |
+
|
271 |
+
# Clean and structure the data
|
272 |
+
df = clean_and_structure_data(issues_data)
|
273 |
+
|
274 |
+
# Perform exploratory data analysis (EDA)
|
275 |
+
perform_eda(df)
|
276 |
+
|
277 |
+
# Analyze text content using NLP
|
278 |
+
analyze_text_content(df)
|
279 |
+
|
280 |
+
# Create a network graph of issues, authors, and assignees
|
281 |
+
create_network_graph(df)
|
282 |
+
|
283 |
+
# Build a predictive model for issue resolution time
|
284 |
+
build_predictive_model(df)
|