Spaces:
Running
Running
Upload folder using huggingface_hub
Browse files
CriticalThinking/app/api/router.py
CHANGED
|
@@ -21,7 +21,7 @@ class AnalyzeRequest(BaseModel):
|
|
| 21 |
class AnalyzeResponse(BaseModel):
|
| 22 |
task_id: str
|
| 23 |
|
| 24 |
-
|
| 25 |
tasks[task_id]["status"] = "processing"
|
| 26 |
try:
|
| 27 |
api_key = os.getenv("OPENAI_API_KEY", "dummy")
|
|
|
|
| 21 |
class AnalyzeResponse(BaseModel):
|
| 22 |
task_id: str
|
| 23 |
|
| 24 |
+
def run_analysis_task(task_id: str, repo_url: Optional[str], project_description: str):
|
| 25 |
tasks[task_id]["status"] = "processing"
|
| 26 |
try:
|
| 27 |
api_key = os.getenv("OPENAI_API_KEY", "dummy")
|
CriticalThinking/app/services/indexer.py
CHANGED
|
@@ -28,13 +28,13 @@ class CodeIndexer:
|
|
| 28 |
temp_dir = tempfile.mkdtemp()
|
| 29 |
try:
|
| 30 |
print(f"Cloning {repo_url} into {temp_dir}...")
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
|
| 39 |
self._index_directory(temp_dir)
|
| 40 |
finally:
|
|
|
|
| 28 |
temp_dir = tempfile.mkdtemp()
|
| 29 |
try:
|
| 30 |
print(f"Cloning {repo_url} into {temp_dir}...")
|
| 31 |
+
# Only allow HTTP/HTTPS URLs for security
|
| 32 |
+
if not repo_url.startswith(("http://", "https://")):
|
| 33 |
+
raise Exception("Only HTTP and HTTPS repository URLs are allowed.")
|
| 34 |
+
|
| 35 |
+
result = subprocess.run(["git", "clone", "--depth", "1", repo_url, temp_dir], capture_output=True, text=True)
|
| 36 |
+
if result.returncode != 0:
|
| 37 |
+
raise Exception(f"Git clone failed: {result.stderr}")
|
| 38 |
|
| 39 |
self._index_directory(temp_dir)
|
| 40 |
finally:
|
CriticalThinking/tests/test_api.py
CHANGED
|
@@ -5,8 +5,13 @@ from unittest.mock import patch, MagicMock
|
|
| 5 |
|
| 6 |
client = TestClient(app)
|
| 7 |
|
|
|
|
| 8 |
@patch("app.api.router.WebResearcher")
|
| 9 |
-
def test_analyze_flow(mock_web_researcher_class):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
# Mock WebResearcher
|
| 11 |
mock_web_researcher = MagicMock()
|
| 12 |
mock_web_researcher_class.return_value = mock_web_researcher
|
|
@@ -14,7 +19,7 @@ def test_analyze_flow(mock_web_researcher_class):
|
|
| 14 |
mock_web_researcher.research_hf_spaces.return_value = "Mocked HF Spaces results"
|
| 15 |
|
| 16 |
# Submit analysis
|
| 17 |
-
response = client.post("/analyze", json={"repo_url": "
|
| 18 |
assert response.status_code == 200
|
| 19 |
task_id = response.json()["task_id"]
|
| 20 |
assert task_id
|
|
|
|
| 5 |
|
| 6 |
client = TestClient(app)
|
| 7 |
|
| 8 |
+
@patch("app.api.router.CodeIndexer")
|
| 9 |
@patch("app.api.router.WebResearcher")
|
| 10 |
+
def test_analyze_flow(mock_web_researcher_class, mock_indexer_class):
|
| 11 |
+
# Mock CodeIndexer
|
| 12 |
+
mock_indexer = MagicMock()
|
| 13 |
+
mock_indexer_class.return_value = mock_indexer
|
| 14 |
+
|
| 15 |
# Mock WebResearcher
|
| 16 |
mock_web_researcher = MagicMock()
|
| 17 |
mock_web_researcher_class.return_value = mock_web_researcher
|
|
|
|
| 19 |
mock_web_researcher.research_hf_spaces.return_value = "Mocked HF Spaces results"
|
| 20 |
|
| 21 |
# Submit analysis
|
| 22 |
+
response = client.post("/analyze", json={"repo_url": "https://github.com/dummy/repo", "project_description": "Test Project"})
|
| 23 |
assert response.status_code == 200
|
| 24 |
task_id = response.json()["task_id"]
|
| 25 |
assert task_id
|
hf_app.py
CHANGED
|
@@ -6,10 +6,40 @@ from CriticalThinking.app.main import app as fastapi_app
|
|
| 6 |
# We can just mount it or use it as the main app.
|
| 7 |
# Here we will mount Gradio onto the existing FastAPI app.
|
| 8 |
|
|
|
|
|
|
|
| 9 |
def analyze_interface(repo_url, project_description):
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
with gr.Blocks(title="Critical Code Agent") as demo:
|
| 15 |
gr.Markdown("# 🦀 Critical Code Agent")
|
|
@@ -23,6 +53,14 @@ with gr.Blocks(title="Critical Code Agent") as demo:
|
|
| 23 |
output = gr.Textbox(label="Status")
|
| 24 |
|
| 25 |
analyze_btn.click(analyze_interface, inputs=[repo_url, project_desc], outputs=output)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
gr.Markdown("### API Endpoints")
|
| 28 |
gr.Markdown("- `POST /analyze`: Submit a repository for analysis")
|
|
|
|
| 6 |
# We can just mount it or use it as the main app.
|
| 7 |
# Here we will mount Gradio onto the existing FastAPI app.
|
| 8 |
|
| 9 |
+
import httpx
|
| 10 |
+
|
| 11 |
def analyze_interface(repo_url, project_description):
|
| 12 |
+
if not repo_url and not project_description:
|
| 13 |
+
return "Please provide at least a project description."
|
| 14 |
+
|
| 15 |
+
try:
|
| 16 |
+
# Call the local FastAPI endpoint
|
| 17 |
+
with httpx.Client() as client:
|
| 18 |
+
response = client.post(
|
| 19 |
+
"http://localhost:7860/analyze",
|
| 20 |
+
json={
|
| 21 |
+
"repo_url": repo_url if repo_url else None,
|
| 22 |
+
"project_description": project_description
|
| 23 |
+
},
|
| 24 |
+
timeout=10.0
|
| 25 |
+
)
|
| 26 |
+
response.raise_for_status()
|
| 27 |
+
data = response.json()
|
| 28 |
+
task_id = data.get("task_id")
|
| 29 |
+
return f"Analysis started! Task ID: {task_id}\n\nYou can check the report at: /report/{task_id}"
|
| 30 |
+
except Exception as e:
|
| 31 |
+
return f"Error starting analysis: {str(e)}"
|
| 32 |
+
|
| 33 |
+
def get_report_interface(task_id):
|
| 34 |
+
if not task_id:
|
| 35 |
+
return "Please provide a Task ID."
|
| 36 |
+
try:
|
| 37 |
+
with httpx.Client() as client:
|
| 38 |
+
response = client.get(f"http://localhost:7860/report/{task_id}")
|
| 39 |
+
response.raise_for_status()
|
| 40 |
+
return response.json()
|
| 41 |
+
except Exception as e:
|
| 42 |
+
return f"Error fetching report: {str(e)}"
|
| 43 |
|
| 44 |
with gr.Blocks(title="Critical Code Agent") as demo:
|
| 45 |
gr.Markdown("# 🦀 Critical Code Agent")
|
|
|
|
| 53 |
output = gr.Textbox(label="Status")
|
| 54 |
|
| 55 |
analyze_btn.click(analyze_interface, inputs=[repo_url, project_desc], outputs=output)
|
| 56 |
+
|
| 57 |
+
gr.Markdown("### Check Report Status")
|
| 58 |
+
with gr.Row():
|
| 59 |
+
task_id_input = gr.Textbox(label="Task ID")
|
| 60 |
+
report_btn = gr.Button("Get Report")
|
| 61 |
+
|
| 62 |
+
report_output = gr.JSON(label="Analysis Report")
|
| 63 |
+
report_btn.click(get_report_interface, inputs=[task_id_input], outputs=report_output)
|
| 64 |
|
| 65 |
gr.Markdown("### API Endpoints")
|
| 66 |
gr.Markdown("- `POST /analyze`: Submit a repository for analysis")
|