Skip to content

Commit e0d24e9

Browse files
committed
Initial commit of LLM deployment project code
0 parents  commit e0d24e9

File tree

2 files changed

+184
-0
lines changed

2 files changed

+184
-0
lines changed

app.py

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
import os
2+
import json
3+
import base64
4+
import tempfile
5+
import subprocess
6+
import time
7+
from uuid import uuid4
8+
from flask import Flask, request, jsonify
9+
import requests
10+
from dotenv import load_dotenv
11+
12+
# Load environment variables (from .env file)
13+
load_dotenv()
14+
15+
app = Flask(__name__)
16+
17+
# ------------------------------------------------------------
18+
# CONFIGURATION
19+
# ------------------------------------------------------------
20+
STUDENT_SECRET = os.getenv("STUDENT_SECRET") # your secret from Google Form
21+
GITHUB_USER = os.getenv("GITHUB_USER") # your GitHub username
22+
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN") # your personal access token
23+
24+
# ------------------------------------------------------------
25+
# HELPER FUNCTIONS
26+
# ------------------------------------------------------------
27+
28+
def verify_secret(secret):
29+
return secret == STUDENT_SECRET
30+
31+
def create_repo(task_name):
32+
"""
33+
Creates a new GitHub repo using GitHub API.
34+
"""
35+
repo_name = f"{task_name}-{uuid4().hex[:6]}"
36+
api_url = "https://api.github.com/user/repos"
37+
headers = {"Authorization": f"token {GITHUB_TOKEN}"}
38+
data = {
39+
"name": repo_name,
40+
"private": False,
41+
"auto_init": True,
42+
"license_template": "mit",
43+
"description": "Auto-generated for LLM Deployment Task"
44+
}
45+
r = requests.post(api_url, headers=headers, json=data)
46+
if r.status_code != 201:
47+
raise Exception(f"Failed to create repo: {r.text}")
48+
return repo_name
49+
50+
def generate_app_files(brief, attachments):
51+
"""
52+
Uses the brief to create a minimal index.html and script.
53+
(Simulating LLM generation)
54+
"""
55+
os.makedirs("../public", exist_ok=True)
56+
57+
# Save attachments locally (if any)
58+
for a in attachments:
59+
fname = a["name"]
60+
data_url = a["url"]
61+
header, encoded = data_url.split(",", 1)
62+
with open(f"../public/{fname}", "wb") as f:
63+
f.write(base64.b64decode(encoded))
64+
65+
# Create a simple web page
66+
html = f"""<!DOCTYPE html>
67+
<html>
68+
<head>
69+
<meta charset="UTF-8">
70+
<title>{brief}</title>
71+
</head>
72+
<body>
73+
<h1>Task: {brief}</h1>
74+
<p>This page was auto-generated for the LLM Deployment project.</p>
75+
</body>
76+
</html>
77+
"""
78+
with open("../public/index.html", "w") as f:
79+
f.write(html)
80+
81+
def git_push(repo_name):
82+
"""
83+
Initializes Git repo in /public and pushes to GitHub.
84+
"""
85+
repo_url = f"https://github.com/{GITHUB_USER}/{repo_name}.git"
86+
subprocess.run(["git", "init"], cwd="../public")
87+
subprocess.run(["git", "remote", "add", "origin", repo_url], cwd="../public")
88+
subprocess.run(["git", "add", "."], cwd="../public")
89+
subprocess.run(["git", "commit", "-m", "Initial commit"], cwd="../public")
90+
subprocess.run(["git", "branch", "-M", "main"], cwd="../public")
91+
subprocess.run(["git", "push", "-u", "origin", "main"], cwd="../public")
92+
return repo_url
93+
94+
def enable_github_pages(repo_name):
95+
"""
96+
Enables GitHub Pages (branch: main, folder: /)
97+
"""
98+
api_url = f"https://api.github.com/repos/{GITHUB_USER}/{repo_name}/pages"
99+
headers = {"Authorization": f"token {GITHUB_TOKEN}"}
100+
data = {"source": {"branch": "main", "path": "/"}}
101+
r = requests.post(api_url, headers=headers, json=data)
102+
if r.status_code not in [201, 204]:
103+
print(f"Warning: GitHub Pages enable failed: {r.text}")
104+
return f"https://{GITHUB_USER}.github.io/{repo_name}/"
105+
106+
# ------------------------------------------------------------
107+
# MAIN API ENDPOINT
108+
# ------------------------------------------------------------
109+
@app.route("/api-endpoint", methods=["GET", "POST"])
110+
def handle_request():
111+
if request.method == "GET":
112+
# Simple response for testing in browser
113+
return "<h1>LLM Deployment API</h1><p>Send a POST request with JSON to use the API.</p>"
114+
115+
# Handle POST requests
116+
data = request.get_json(force=True)
117+
print("Received task:", data)
118+
119+
# Verify secret
120+
if not verify_secret(data.get("secret", "")):
121+
return jsonify({"error": "Invalid secret"}), 403
122+
123+
# Extract request data
124+
email = data.get("email")
125+
brief = data.get("brief")
126+
attachments = data.get("attachments", [])
127+
evaluation_url = data.get("evaluation_url")
128+
task = data.get("task")
129+
nonce = data.get("nonce")
130+
round_no = data.get("round")
131+
132+
try:
133+
# Step 1: Generate app
134+
generate_app_files(brief, attachments)
135+
136+
# Step 2: Create GitHub repo
137+
repo_name = create_repo(task)
138+
139+
# Step 3: Push generated files
140+
repo_url = git_push(repo_name)
141+
142+
# Step 4: Enable GitHub Pages
143+
pages_url = enable_github_pages(repo_name)
144+
145+
# Step 5: Send evaluation payload
146+
payload = {
147+
"email": email,
148+
"task": task,
149+
"round": round_no,
150+
"nonce": nonce,
151+
"repo_url": repo_url,
152+
"commit_sha": "main",
153+
"pages_url": pages_url
154+
}
155+
156+
headers = {"Content-Type": "application/json"}
157+
resp = requests.post(evaluation_url, headers=headers, json=payload)
158+
print("Evaluation response:", resp.status_code)
159+
160+
return jsonify({
161+
"status": "success",
162+
"repo_url": repo_url,
163+
"pages_url": pages_url
164+
})
165+
166+
except Exception as e:
167+
print("Error:", e)
168+
return jsonify({"error": str(e)}), 500
169+
170+
171+
# ------------------------------------------------------------
172+
# ENTRY POINT
173+
# ------------------------------------------------------------
174+
if __name__ == "__main__":
175+
app.run(host="0.0.0.0", port=8000)

requirements.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
flask
2+
requests
3+
python-dotenv
4+
5+
fastapi==0.111.1
6+
uvicorn==0.24.0
7+
torch==2.8.0
8+
transformers==4.41.0
9+
python-dotenv==1.0.0

0 commit comments

Comments
 (0)