-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fcb9d0b
commit a6ad47a
Showing
5 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
FROM python:3.11-slim as build | ||
|
||
WORKDIR /app | ||
|
||
COPY . . | ||
|
||
ARG NOCO_DB_URL | ||
ARG NOCO_XC_TOKEN | ||
ARG LLM_URL | ||
|
||
ENV NOCO_DB_URL=${NOCO_DB_URL} | ||
ENV NOCO_XC_TOKEN=${NOCO_XC_TOKEN} | ||
ENV LLM_URL=${LLM_URL} | ||
|
||
RUN pip install --no-cache-dir -r requirements.txt | ||
|
||
EXPOSE 5001 | ||
|
||
CMD ["python", "./app.py"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
from flask import Flask | ||
from apscheduler.schedulers.background import BackgroundScheduler | ||
from dotenv import load_dotenv | ||
|
||
import os | ||
import requests | ||
import json | ||
|
||
app = Flask(__name__) | ||
scheduler = BackgroundScheduler() | ||
|
||
@app.route('/health') | ||
def health_check(): | ||
return 'healthy' | ||
|
||
def classify(): | ||
scheduler.pause() | ||
|
||
while True: | ||
db_url = os.getenv("NOCO_DB_URL") | ||
headers = {"xc-token": os.getenv("NOCO_XC_TOKEN")} | ||
params = { | ||
"where": "(status,eq,unverified)~and(isEnglish,eq,false)", | ||
"limit": 5, | ||
} | ||
|
||
llm_url = os.getenv("LLM_URL") | ||
form_data = { | ||
"variables": "headline,body", | ||
"string_prompt": "<<SYS>> \n You are an assistant tasked with classifying whether the given headline and body is related to China Development Bank or China Import Export Bank or Debt or Loan From China. \n <</SYS>> \n\n [INST] Generate a SHORT response if the given headline and article body is related to China. The output should be either Yes or No. \n\n Headline: \n\n {headline} \n\n Body: {body}\n\n [/INST]", | ||
} | ||
|
||
articles = requests.get(db_url, headers=headers, params=params) | ||
if res.json().get("pageInfo").get("totalRows") == 0: | ||
print("[MOF Classifier] No articles to classify") | ||
scheduler.resume() | ||
break | ||
|
||
for article in articles.json().get("list"): | ||
attempts = 3 | ||
while attempts > 0: | ||
try: | ||
print("Article to classify: " + article["originalTitle"]) | ||
form_data["llm_request"] = json.dumps({ | ||
"headline": article["originalTitle"], | ||
"body": article["originalContent"] | ||
}) | ||
|
||
res = requests.post(llm_url, data=form_data) | ||
|
||
if "yes" in res.json().get("Result").lower()[:5]: | ||
article["status"] = "relevant" | ||
else: | ||
article["status"] = "irrelevant" | ||
|
||
break | ||
except: | ||
attempts -= 1 | ||
article["status"] = "undetermined" | ||
|
||
requests.patch(db_url, headers=headers, json=article) | ||
|
||
if __name__ == '__main__': | ||
load_dotenv() | ||
scheduler.add_job(classify, "cron", hour="*", minute=30) | ||
scheduler.start() | ||
app.run(port=5003) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
APScheduler | ||
flask | ||
python-dotenv | ||
requests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters