Spaces:
Running
Running
Commit
·
a8e6c78
1
Parent(s):
42dcb6c
FIRST COMMIT LESGO EZCLAP
Browse files- .gitignore +3 -0
- Dockerfile +16 -0
- jwt_setup.py +69 -0
- server.py +49 -0
.gitignore
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
.env
|
2 |
+
venv/
|
3 |
+
__pycache__
|
Dockerfile
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3
|
2 |
+
|
3 |
+
RUN useradd -m -u 1000 user
|
4 |
+
USER user
|
5 |
+
ENV HOME=/home/user \
|
6 |
+
PATH=/home/user/.local/bin:$PATH
|
7 |
+
|
8 |
+
COPY --chown=user . $HOME/github_bot
|
9 |
+
|
10 |
+
WORKDIR $HOME/github_bot
|
11 |
+
|
12 |
+
RUN mkdir $HOME/.cache
|
13 |
+
|
14 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
15 |
+
|
16 |
+
CMD ["gunicorn", "-w", "5", "-b", "0.0.0.0:7860","app:app"]
|
jwt_setup.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datetime import datetime
|
2 |
+
import jwt
|
3 |
+
import requests
|
4 |
+
import base64
|
5 |
+
|
6 |
+
|
7 |
+
def decode_string(base64_string: str) -> str:
|
8 |
+
"""Decodes a Base64 string.
|
9 |
+
|
10 |
+
NOTE:
|
11 |
+
This workaround was required because .env cannot handle multi-line strings, so it was required to convert it to a single line. There are mutliple ways to achieve the same result.
|
12 |
+
"""
|
13 |
+
|
14 |
+
base64_bytes = base64_string.encode('utf-8')
|
15 |
+
string_bytes = base64.b64decode(base64_bytes)
|
16 |
+
string = string_bytes.decode('utf-8')
|
17 |
+
return string
|
18 |
+
|
19 |
+
|
20 |
+
def get_installation_access_token(
|
21 |
+
signing_key: str, app_id: str, installation_id: str
|
22 |
+
) -> str:
|
23 |
+
"""
|
24 |
+
Obtain and return a GitHub installation access token.
|
25 |
+
|
26 |
+
Arguments:
|
27 |
+
signing_key: base64 encoded signing key (you'll get this from the .env)
|
28 |
+
app_id: The application ID
|
29 |
+
installation_id: The ID of the app installation.
|
30 |
+
|
31 |
+
Returns:
|
32 |
+
The installation access token obtained from GitHub.
|
33 |
+
"""
|
34 |
+
|
35 |
+
# Refer https://github.com/orgs/community/discussions/48186
|
36 |
+
# Refer https://stackoverflow.com/questions/77325437/how-do-i-get-an-github-app-installation-token-to-authenticate-cloning-a-reposito
|
37 |
+
now = int(datetime.now().timestamp())
|
38 |
+
|
39 |
+
signing_key = decode_string(signing_key)
|
40 |
+
signing_key = jwt.jwk_from_pem(signing_key.encode())
|
41 |
+
payload = {"iat": now, "exp": now + 600, "iss": app_id}
|
42 |
+
jwt_instance = jwt.JWT()
|
43 |
+
encoded_jwt = jwt_instance.encode(payload, signing_key, alg="RS256")
|
44 |
+
|
45 |
+
response = requests.post(
|
46 |
+
"https://api.github.com/app/installations/" f"{installation_id}/access_tokens",
|
47 |
+
headers={
|
48 |
+
"Authorization": f"Bearer {encoded_jwt}",
|
49 |
+
"Accept": "application/vnd.github+json",
|
50 |
+
"X-GitHub-Api-Version": "2022-11-28",
|
51 |
+
},
|
52 |
+
)
|
53 |
+
if not 200 <= response.status_code < 300:
|
54 |
+
raise RuntimeError(
|
55 |
+
"Unable to get token. Status code was "
|
56 |
+
f"{response.status_code}, body was {response.text}."
|
57 |
+
)
|
58 |
+
|
59 |
+
return response.json()["token"]
|
60 |
+
|
61 |
+
|
62 |
+
if __name__ == "__main__":
|
63 |
+
import os
|
64 |
+
|
65 |
+
print(get_installation_access_token(
|
66 |
+
os.environ.get('CHATBOT_PUB_KEY'),
|
67 |
+
os.environ.get('APP_ID'),
|
68 |
+
os.environ.get('INSTALLATION_ID')
|
69 |
+
))
|
server.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request
|
2 |
+
import requests
|
3 |
+
import json
|
4 |
+
import os
|
5 |
+
|
6 |
+
from jwt_setup import get_installation_access_token
|
7 |
+
|
8 |
+
|
9 |
+
app = Flask(__name__)
|
10 |
+
|
11 |
+
def get_llm_output(query: str):
|
12 |
+
LLM_output = requests.post('https://robinroy03-fury-engine.hf.space/api/google/generate', json={"query": query, "llm": "gemini-1.5-pro", "knn": 3, "stream": False})
|
13 |
+
LLM_output = json.loads(LLM_output.text)
|
14 |
+
LLM_output = LLM_output['response'] + '\n\n**References**\n' + LLM_output['references']
|
15 |
+
|
16 |
+
return LLM_output
|
17 |
+
|
18 |
+
|
19 |
+
def post_discussion_comment(discussion_id: str, body: str):
|
20 |
+
token = get_installation_access_token(
|
21 |
+
os.environ.get('CHATBOT_PUB_KEY'),
|
22 |
+
os.environ.get('APP_ID'),
|
23 |
+
os.environ.get('INSTALLATION_ID')
|
24 |
+
)
|
25 |
+
|
26 |
+
headers = {"Authorization": f"token {token}"}
|
27 |
+
query = "mutation AddDiscussionComment($discussionId: ID!, $body: String!) {addDiscussionComment(input: {discussionId: $discussionId, body: $body}) {clientMutationId}}"
|
28 |
+
variables = {"discussionId": discussion_id, "body": body}
|
29 |
+
|
30 |
+
requests.post('https://api.github.com/graphql', json={'query': query, 'variables': variables}, headers=headers)
|
31 |
+
|
32 |
+
|
33 |
+
@app.route("/", methods=['POST'])
|
34 |
+
def main():
|
35 |
+
payload = request.get_json()
|
36 |
+
URL = "https://api.github.com/graphql"
|
37 |
+
|
38 |
+
if 'discussion' in payload and 'comment' not in payload:
|
39 |
+
discussion_id = payload['discussion']['node_id']
|
40 |
+
discussion_title = payload['discussion']['title']
|
41 |
+
discussion_body = payload['discussion']['body']
|
42 |
+
print(discussion_id)
|
43 |
+
|
44 |
+
post_discussion_comment(discussion_id=discussion_id, body=get_llm_output(f"{discussion_title}\n{discussion_body}"))
|
45 |
+
|
46 |
+
return {"status": True}
|
47 |
+
|
48 |
+
else:
|
49 |
+
return {"status": False}
|