Arvind2006 commited on
Commit
4a42026
·
verified ·
1 Parent(s): 657c233

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +33 -128
app.py CHANGED
@@ -1,133 +1,38 @@
1
- # app.py - Entry point for HuggingFace Spaces (FastAPI)
2
-
3
  import sys
4
  sys.path.insert(0, ".")
5
 
6
- from fastapi import FastAPI
7
- from fastapi.responses import HTMLResponse
8
- from pydantic import BaseModel
9
- from explain_error import explain_error
10
-
11
- # -------------------------
12
- # FastAPI App
13
- # -------------------------
14
- app = FastAPI(title="Jenkins Error Explainer")
15
-
16
- # -------------------------
17
- # Request Schema
18
- # -------------------------
19
- class LogRequest(BaseModel):
20
- log_text: str
21
-
22
- # -------------------------
23
- # Home Route (HF Health Check Compatible)
24
- # -------------------------
25
- @app.get("/")
26
- async def home():
27
- return {"message": "Jenkins Error Explainer API running", "status": "ok"}
28
-
29
- @app.get("/health")
30
- def health():
31
- return {"status": "ok"}
32
-
33
- # -------------------------
34
- # HTML UI Route
35
- # -------------------------
36
- @app.get("/ui", response_class=HTMLResponse)
37
- async def ui():
38
- return """<!DOCTYPE html>
39
- <html lang="en">
40
- <head>
41
- <meta charset="UTF-8">
42
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
43
- <title>Jenkins Error Explainer</title>
44
- <style>
45
- * { box-sizing: border-box; margin: 0; padding: 0; }
46
- body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; background: #f5f5f5; padding: 20px; }
47
- .container { max-width: 800px; margin: 0 auto; }
48
- h1 { color: #333; margin-bottom: 10px; }
49
- .description { color: #666; margin-bottom: 20px; }
50
- textarea { width: 100%; height: 200px; padding: 15px; border: 1px solid #ddd; border-radius: 8px; font-family: monospace; font-size: 14px; resize: vertical; }
51
- button { background: #4CAF50; color: white; border: none; padding: 12px 24px; font-size: 16px; border-radius: 8px; cursor: pointer; margin-top: 10px; }
52
- button:hover { background: #45a049; }
53
- #result { margin-top: 20px; padding: 20px; background: white; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }
54
- .error-category { color: #e74c3c; font-weight: bold; font-size: 18px; }
55
- .summary { margin: 15px 0; line-height: 1.6; }
56
- .causes ul { margin-left: 20px; }
57
- .references { margin-top: 15px; font-size: 14px; color: #666; }
58
- </style>
59
- </head>
60
- <body>
61
- <div class="container">
62
- <h1>🔧 Jenkins Error Explainer</h1>
63
- <p class="description">AI-powered Jenkins pipeline error diagnosis using RAG</p>
64
-
65
- <textarea id="logInput" placeholder="Paste your Jenkins error log here..."></textarea>
66
- <br>
67
- <button onclick="explainError()" id="submitBtn">Explain Error</button>
68
-
69
- <div id="result"></div>
70
- </div>
71
-
72
- <script>
73
- async function explainError() {
74
- const logText = document.getElementById('logInput').value;
75
- const resultDiv = document.getElementById('result');
76
- const btn = document.getElementById('submitBtn');
77
-
78
- if (!logText.trim()) {
79
- resultDiv.innerHTML = '<p>Please provide a Jenkins error log.</p>';
80
- return;
81
- }
82
-
83
- btn.disabled = true;
84
- btn.textContent = 'Analyzing...';
85
- resultDiv.innerHTML = '<p>Analyzing your Jenkins error...</p>';
86
-
87
- try {
88
- const response = await fetch('/explain', {
89
- method: 'POST',
90
- headers: { 'Content-Type': 'application/json' },
91
- body: JSON.stringify({ log_text: logText })
92
- });
93
-
94
- const data = await response.json();
95
-
96
- resultDiv.innerHTML = `
97
- <p class="error-category">Error Category: ${data.error_category}</p>
98
- <p class="summary"><strong>Summary:</strong> ${data.summary}</p>
99
- <div class="causes">
100
- <strong>Likely Causes:</strong>
101
- <ul>${data.likely_causes.map(c => `<li>${c}</li>`).join('')}</ul>
102
- </div>
103
- <div class="references">
104
- <strong>References:</strong><br>
105
- ${data.references.join('<br>')}
106
- </div>
107
- `;
108
- } catch (e) {
109
- resultDiv.innerHTML = '<p>Error connecting to API. Please try again.</p>';
110
- }
111
-
112
- btn.disabled = false;
113
- btn.textContent = 'Explain Error';
114
- }
115
- </script>
116
- </body>
117
- </html>"""
118
- @app.get("/health")
119
- def health():
120
- return {"status": "ok"}
121
- # -------------------------
122
- # API Endpoint
123
- # -------------------------
124
- @app.post("/explain")
125
- async def explain(payload: LogRequest):
126
- return explain_error(payload.log_text)
127
 
128
- # -------------------------
129
- # Entrypoint
130
- # -------------------------
131
  if __name__ == "__main__":
132
- import uvicorn
133
- uvicorn.run("app:app", host="0.0.0.0", port=7860)
 
1
+ # app.py - Entry point for HuggingFace Spaces
 
2
  import sys
3
  sys.path.insert(0, ".")
4
 
5
+ import gradio as gr
6
+ from main import app
7
+ import uvicorn
8
+ import threading
9
+ import requests
10
+ import json
11
+
12
+ # Start FastAPI server in background
13
+ def start_fastapi():
14
+ uvicorn.run(app, host="0.0.0.0", port=7860)
15
+
16
+ # Start FastAPI in a separate thread
17
+ threading.Thread(target=start_fastapi, daemon=True).start()
18
+
19
+ # Gradio interface
20
+ def explain_error_gradio(log_text):
21
+ try:
22
+ response = requests.post("http://localhost:7860/explain",
23
+ json={"log_text": log_text})
24
+ return json.dumps(response.json(), indent=2)
25
+ except Exception as e:
26
+ return f"Error: {str(e)}"
27
+
28
+ # Create Gradio interface
29
+ iface = gr.Interface(
30
+ fn=explain_error_gradio,
31
+ inputs=gr.Textbox(lines=10, placeholder="Paste your Jenkins error log here..."),
32
+ outputs=gr.Textbox(lines=15, label="Error Explanation"),
33
+ title="Jenkins Error Explainer",
34
+ description="AI-powered Jenkins error explanation using RAG"
35
+ )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
 
 
 
 
37
  if __name__ == "__main__":
38
+ iface.launch()