Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,3 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
from fastapi import FastAPI, HTTPException
|
5 |
from pydantic import BaseModel
|
6 |
import requests
|
@@ -17,7 +14,7 @@ logger = logging.getLogger(__name__)
|
|
17 |
# Load environment variables
|
18 |
load_dotenv()
|
19 |
|
20 |
-
# Configuration
|
21 |
GITHUB_TOKEN = "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"
|
22 |
GROQ_API_KEY = "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"
|
23 |
REPOSITORIES = [
|
@@ -66,18 +63,18 @@ def fetch_repository_changes(repo: str, days_back: int) -> list[str]:
|
|
66 |
# Fetch commits
|
67 |
commits_url = f"{GITHUB_API_URL}/repos/{repo}/commits"
|
68 |
commits_params = {"since": since_date}
|
69 |
-
logger.debug(f"Fetching commits from: {commits_url}")
|
70 |
commits_response = requests.get(commits_url, headers=headers, params=commits_params)
|
71 |
-
commits_response.raise_for_status()
|
72 |
commits = commits_response.json()
|
73 |
logger.debug(f"Found {len(commits)} commits for {repo}")
|
74 |
|
75 |
# Fetch pull requests
|
76 |
prs_url = f"{GITHUB_API_URL}/repos/{repo}/pulls"
|
77 |
prs_params = {"state": "all", "sort": "updated", "direction": "desc"}
|
78 |
-
logger.debug(f"Fetching pull requests from: {prs_url}")
|
79 |
prs_response = requests.get(prs_url, headers=headers, params=prs_params)
|
80 |
-
prs_response.raise_for_status()
|
81 |
prs = prs_response.json()
|
82 |
logger.debug(f"Found {len(prs)} pull requests for {repo}")
|
83 |
|
@@ -86,7 +83,9 @@ def fetch_repository_changes(repo: str, days_back: int) -> list[str]:
|
|
86 |
for commit in commits:
|
87 |
changes.append(f"Commit: {commit['commit']['message']}")
|
88 |
for pr in prs:
|
89 |
-
|
|
|
|
|
90 |
|
91 |
logger.debug(f"Total changes for {repo}: {len(changes)}")
|
92 |
return changes
|
@@ -102,11 +101,14 @@ def summarize_changes_with_deepseek(repo: str, changes: list[str]) -> dict:
|
|
102 |
try:
|
103 |
logger.debug(f"Summarizing changes for repository: {repo}")
|
104 |
prompt = f"""
|
105 |
-
|
106 |
-
|
107 |
-
|
|
|
|
|
|
|
108 |
"""
|
109 |
-
logger.debug(f"Sending prompt to DeepSeek: {prompt}")
|
110 |
response = groq_client.chat.completions.create(
|
111 |
model="deepseek-chat",
|
112 |
messages=[{"role": "user", "content": prompt}],
|
@@ -114,18 +116,18 @@ def summarize_changes_with_deepseek(repo: str, changes: list[str]) -> dict:
|
|
114 |
temperature=0.7
|
115 |
)
|
116 |
summary = response.choices[0].message.content
|
117 |
-
logger.debug(f"Received summary from DeepSeek: {summary}")
|
118 |
|
119 |
-
# Extract description and context
|
120 |
-
description =
|
121 |
-
context =
|
122 |
-
|
123 |
-
|
|
|
|
|
|
|
124 |
|
125 |
-
return {
|
126 |
-
"description": description,
|
127 |
-
"context": context
|
128 |
-
}
|
129 |
|
130 |
except Exception as e:
|
131 |
logger.error(f"Error summarizing changes for {repo}: {e}")
|
@@ -147,7 +149,7 @@ async def monitor_repositories():
|
|
147 |
logger.debug(f"Summarizing changes for {repo}")
|
148 |
summary = summarize_changes_with_deepseek(repo, changes)
|
149 |
results.append(RepositoryDetails(
|
150 |
-
repo_name=f"{repo} (+{len(changes)}
|
151 |
repo_url=f"https://github.com/{repo}",
|
152 |
changes="\n".join(changes),
|
153 |
description=summary["description"],
|
@@ -168,4 +170,8 @@ async def monitor_repositories():
|
|
168 |
|
169 |
except Exception as e:
|
170 |
logger.error(f"Error in monitor_repositories: {e}")
|
171 |
-
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from fastapi import FastAPI, HTTPException
|
2 |
from pydantic import BaseModel
|
3 |
import requests
|
|
|
14 |
# Load environment variables
|
15 |
load_dotenv()
|
16 |
|
17 |
+
# Configuration (Use environment variables instead of hardcoding)
|
18 |
GITHUB_TOKEN = "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"
|
19 |
GROQ_API_KEY = "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"
|
20 |
REPOSITORIES = [
|
|
|
63 |
# Fetch commits
|
64 |
commits_url = f"{GITHUB_API_URL}/repos/{repo}/commits"
|
65 |
commits_params = {"since": since_date}
|
66 |
+
logger.debug(f"Fetching commits from: {commits_url} with params: {commits_params}")
|
67 |
commits_response = requests.get(commits_url, headers=headers, params=commits_params)
|
68 |
+
commits_response.raise_for_status()
|
69 |
commits = commits_response.json()
|
70 |
logger.debug(f"Found {len(commits)} commits for {repo}")
|
71 |
|
72 |
# Fetch pull requests
|
73 |
prs_url = f"{GITHUB_API_URL}/repos/{repo}/pulls"
|
74 |
prs_params = {"state": "all", "sort": "updated", "direction": "desc"}
|
75 |
+
logger.debug(f"Fetching pull requests from: {prs_url} with params: {prs_params}")
|
76 |
prs_response = requests.get(prs_url, headers=headers, params=prs_params)
|
77 |
+
prs_response.raise_for_status()
|
78 |
prs = prs_response.json()
|
79 |
logger.debug(f"Found {len(prs)} pull requests for {repo}")
|
80 |
|
|
|
83 |
for commit in commits:
|
84 |
changes.append(f"Commit: {commit['commit']['message']}")
|
85 |
for pr in prs:
|
86 |
+
updated_at = datetime.strptime(pr["updated_at"], "%Y-%m-%dT%H:%M:%SZ")
|
87 |
+
if updated_at >= datetime.now() - timedelta(days=days_back):
|
88 |
+
changes.append(f"PR: {pr['title']} - {pr['body'] or 'No description'}")
|
89 |
|
90 |
logger.debug(f"Total changes for {repo}: {len(changes)}")
|
91 |
return changes
|
|
|
101 |
try:
|
102 |
logger.debug(f"Summarizing changes for repository: {repo}")
|
103 |
prompt = f"""
|
104 |
+
Analyze the following changes made to detection rules in the GitHub repository {repo}:
|
105 |
+
{', '.join(changes)}
|
106 |
+
|
107 |
+
Provide a detailed response with two sections:
|
108 |
+
- Description: Summarize what changes were made.
|
109 |
+
- Context: Explain why these changes might be required.
|
110 |
"""
|
111 |
+
logger.debug(f"Sending prompt to DeepSeek: {prompt[:100]}...") # Truncate for brevity in logs
|
112 |
response = groq_client.chat.completions.create(
|
113 |
model="deepseek-chat",
|
114 |
messages=[{"role": "user", "content": prompt}],
|
|
|
116 |
temperature=0.7
|
117 |
)
|
118 |
summary = response.choices[0].message.content
|
119 |
+
logger.debug(f"Received summary from DeepSeek: {summary[:100]}...")
|
120 |
|
121 |
+
# Extract description and context with fallback
|
122 |
+
description = "Description not found."
|
123 |
+
context = "Context not found."
|
124 |
+
if "Description:" in summary and "Context:" in summary:
|
125 |
+
description = summary.split("Description:")[1].split("Context:")[0].strip()
|
126 |
+
context = summary.split("Context:")[1].strip()
|
127 |
+
else:
|
128 |
+
description = summary # Fallback to full summary if sections aren't clear
|
129 |
|
130 |
+
return {"description": description, "context": context}
|
|
|
|
|
|
|
131 |
|
132 |
except Exception as e:
|
133 |
logger.error(f"Error summarizing changes for {repo}: {e}")
|
|
|
149 |
logger.debug(f"Summarizing changes for {repo}")
|
150 |
summary = summarize_changes_with_deepseek(repo, changes)
|
151 |
results.append(RepositoryDetails(
|
152 |
+
repo_name=f"{repo} (+{len(changes)})",
|
153 |
repo_url=f"https://github.com/{repo}",
|
154 |
changes="\n".join(changes),
|
155 |
description=summary["description"],
|
|
|
170 |
|
171 |
except Exception as e:
|
172 |
logger.error(f"Error in monitor_repositories: {e}")
|
173 |
+
raise HTTPException(status_code=500, detail=str(e))
|
174 |
+
|
175 |
+
if __name__ == "__main__":
|
176 |
+
import uvicorn
|
177 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|