VETRIVEL23/llm-code-api
0
1import gradio as gr
2import threading
3import requests
4import os
5import uuid
6from github import Github
7from dotenv import load_dotenv
8import base64
9
10# Load secrets
11load_dotenv()
12GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
13STUDENT_SECRET = os.getenv("STUDENT_SECRET")
14
15g = Github(GITHUB_TOKEN)
16
17# --- Utilities ---
18def create_repo(task_name, brief, attachments=[]):
19 user = g.get_user()
20 repo_name = f"{task_name}-{str(uuid.uuid4())[:5]}"
21 repo = user.create_repo(repo_name, private=False)
22
23 # Add LICENSE
24 license_text = """MIT License
25
26Copyright (c) 2025 Student
27
28Permission is hereby granted, free of charge, to any person obtaining a copy..."""
29 repo.create_file("LICENSE", "Add MIT License", license_text)
30
31 # Add README
32 readme_text = f"# {repo_name}\n\nTask Brief:\n{brief}\n\nLicense: MIT"
33 repo.create_file("README.md", "Add README.md", readme_text)
34
35 # Add attachments (like data.csv)
36 for att in attachments:
37 name = att["name"]
38 data = base64.b64decode(att["content"])
39 repo.create_file(name, f"Add {name}", data.decode())
40
41 return repo
42
43def update_repo(repo_url, brief, round2_files=[]):
44 user = g.get_user()
45 repo_name = repo_url.split("/")[-1]
46 repo = user.get_repo(repo_name)
47
48 # Update README for round2
49 readme_file = repo.get_contents("README.md")
50 updated_readme = readme_file.decoded_content.decode() + f"\n\nRound 2 Update:\n{brief}"
51 repo.update_file("README.md", "Update README for round 2", updated_readme, readme_file.sha)
52
53 # Add additional files for round2
54 for f in round2_files:
55 name = f["name"]
56 content = f["content"]
57 try:
58 repo.create_file(name, f"Add {name}", content)
59 except:
60 # If file exists, update it
61 file_obj = repo.get_contents(name)
62 repo.update_file(name, f"Update {name}", content, file_obj.sha)
63
64 return repo
65
66# --- Delayed processing ---
67def process_task(data):
68 try:
69 task_name = data.get("task")
70 round_number = data.get("round")
71 brief = data.get("brief", "")
72 attachments = data.get("attachments", [])
73 repo_url = data.get("repo_url")
74 evaluation_url = data.get("evaluation_url")
75 round2_files = data.get("round2_files", [])
76
77 if round_number == 1:
78 repo = create_repo(task_name, brief, attachments)
79 else:
80 repo = update_repo(repo_url, brief, round2_files)
81
82 repo_url = repo.html_url
83 commit_sha = repo.get_commits()[0].sha
84 pages_url = f"https://{repo.owner.login}.github.io/{repo.name}/"
85
86 # Send POST to evaluation_url
87 payload = {
88 "email": data.get("email"),
89 "task": task_name,
90 "round": round_number,
91 "nonce": data.get("nonce"),
92 "repo_url": repo_url,
93 "commit_sha": commit_sha,
94 "pages_url": pages_url
95 }
96 if evaluation_url:
97 requests.post(evaluation_url, json=payload)
98
99 except Exception as e:
100 print("Error processing task:", e)
101
102# --- Gradio Interface ---
103def handle_input(json_text):
104 try:
105 data = eval(json_text) # convert string to dict (you can use json.loads if safer)
106 except Exception:
107 return {"error": "Invalid JSON format"}
108
109 # Verify secret
110 if data.get("secret") != STUDENT_SECRET:
111 return {"error": "Invalid secret"}
112
113 # Immediately respond with usercode
114 usercode = str(uuid.uuid4())
115 response = {"usercode": usercode}
116
117 # Start delayed thread to process repo & evaluation POST
118 threading.Thread(target=process_task, args=(data,)).start()
119
120 return response
121
122iface = gr.Interface(
123 fn=handle_input,
124 inputs=gr.Textbox(label="Paste JSON Request Here", lines=20),
125 outputs=gr.JSON(label="Response"),
126 title="Student API Receiver"
127)
128
129iface.launch(server_name="0.0.0.0", server_port=7860)
130 