-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
259 lines (193 loc) · 7.89 KB
/
Copy pathmain.py
File metadata and controls
259 lines (193 loc) · 7.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import csv
import hashlib
import hmac
import json
import os
import uuid
from pathlib import Path
from typing import Annotated, List
import numpy as np
import requests
import uvicorn
from dotenv import load_dotenv
from fastapi import BackgroundTasks, FastAPI, Form, UploadFile, File
from fastapi.middleware.cors import CORSMiddleware
from fastapi.staticfiles import StaticFiles
from jobspy import scrape_jobs
from pydantic import BaseModel
from analyst import Analyzer
from archetype_chatbot import ArchetypeChatbot
from cv_analyst import GeminiCVAnalyst
def convert_int64(o):
if isinstance(o, np.int64):
return int(o)
raise TypeError
load_dotenv()
def send_webhook(event, data):
secret = os.getenv("WEBHOOK_SECRET", "very-long-secret")
url = os.getenv("WEBHOOK_URL", "http://localhost:8002/webhook")
payload = {"event": event, "data": data}
payload_string = json.dumps(payload)
payload_bytes = json.dumps(payload).encode("utf-8")
secret_bytes = bytes(secret, "utf-8")
signature = hmac.new(secret_bytes, payload_bytes, hashlib.sha256)
headers = {
"Content-Type": "application/json",
"Signature": signature.hexdigest(),
}
print(
f"Sending webhook to {url} with payload {payload_string} and headers {headers}"
)
response = requests.post(url, data=payload_string, headers=headers)
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
public_dir = Path(__file__).parent / "public"
if not public_dir.exists():
raise RuntimeError(f"Directory '{public_dir}' does not exist")
app.mount("/public", StaticFiles(directory=public_dir), name="public")
class TextSubmission(BaseModel):
text: str
jobs_analysis_id: int
job_lists_id: int
mode: str = "default"
def analyze_task(submission: TextSubmission):
print(f"Received submission: {submission.text}")
jobs = scrape_jobs(
site_name=["indeed", "linkedin", "zip_recruiter"],
search_term=submission.text, # Mengakses `text` dari objek submission
location="indonesia",
results_wanted=1000,
hours_old=24 * 30 * 12,
country_indeed="indonesia",
)
print(f"Found {len(jobs)} jobs")
jobs_file_name = "public/" + str(uuid.uuid4()) + ".csv"
jobs.to_csv(
jobs_file_name, quoting=csv.QUOTE_NONNUMERIC, escapechar="\\", index=False
)
print(f"Saved jobs to {jobs_file_name}")
analyst = Analyzer(csv_dir="./" + jobs_file_name)
print("Analysing data...")
analysis_res = {
"top_job_titles": analyst.top_job_titles(),
"wordcloud_data": analyst.wordcloud(),
"top10_job_locs": analyst.top10_job_locations(),
"job_post_trend": analyst.job_posting_trend(),
"top10_industries_with_most_jobs": analyst.top10_industries_with_most_jobs(),
"most_mentioned_skills_and_techstacks": analyst.most_mentioned_skills_and_techstacks(),
"top10_remote_jobs": analyst.top10_remote_jobs(),
"top10_non_remote_jobs": analyst.top10_non_remote_jobs(),
"tech_stacks_overtime": analyst.tech_stacks_overtime(),
}
print("Analysis done")
json_res_name = "public/" + str(uuid.uuid4()) + ".json"
with open(json_res_name, "w") as json_file:
json.dump(analysis_res, json_file, indent=4, default=convert_int64)
print(f"Saved analysis to {json_res_name}")
response = {
"jobs_file": jobs_file_name,
"analysis_file": json_res_name,
"jobs_analysis_id": submission.jobs_analysis_id,
"job_lists_id": submission.job_lists_id,
}
send_webhook("analysis_generated", response)
@app.post("/generate_analysis")
async def analyze(submission: TextSubmission, background_tasks: BackgroundTasks):
background_tasks.add_task(analyze_task, submission)
return {"message": "Analysis started"}
def analyze_cv_task(input_text, json_data, review_id: str):
cv_analyst = GeminiCVAnalyst()
result = cv_analyst.run_cv_analyst(input_text, json_data)
response = {
"result": result,
"review_id": review_id,
}
send_webhook("cv_analyzed", response)
@app.post("/analyze_cv")
async def analyze_cv(
background_tasks: BackgroundTasks,
file: UploadFile,
job_analysis: UploadFile,
review_id: Annotated[str, Form()],
):
print(
f"Received CV file: {file.filename} and job analysis file: {job_analysis.filename} with review_id: {review_id}"
)
input_text = await file.read()
json_data = json.load(job_analysis.file)
background_tasks.add_task(analyze_cv_task, input_text, json_data, review_id)
return {"message": "CV analysis started"}
#################################### Bagian ini baru ####################################################
class QuizItem(BaseModel):
question: str
answer: str
userAnswer: str
class QuizResult(BaseModel):
feedback: str
nilai: float
chatbot = ArchetypeChatbot()
@app.post("/upskill-judge", response_model=List[QuizResult])
async def upskill_judge(quiz_items: List[QuizItem]):
all_questions = [
{
"question": item.question,
"correct_answer": item.answer,
"user_answer": item.userAnswer,
}
for item in quiz_items
]
input_text = json.dumps({"questions": all_questions})
processed_results = chatbot.process_text(input_text)
results = []
if isinstance(processed_results, list):
for result in processed_results:
feedback = result.get("Komentar", "Tidak ada feedback.")
nilai = result.get("Nilai", 0)
results.append(QuizResult(feedback=feedback, nilai=nilai))
else:
print(f"Unexpected response format: {processed_results}")
raise ValueError("Response format is not as expected.")
return results
import webrtcvad
import soundfile as sf
import io
from fastapi.responses import JSONResponse
NOISE_THRESHOLD = 0.02
VAD_AGGRESSIVENESS = 2
vad = webrtcvad.Vad(VAD_AGGRESSIVENESS)
def is_noisy(audio_data, threshold):
rms = np.sqrt(np.mean(np.square(audio_data)))
return 1 if rms > threshold else 0
def is_speech(audio_data, sample_rate):
audio_bytes = (audio_data * 32768).astype(np.int16).tobytes()
frame_duration = 30
frame_size = int(sample_rate * frame_duration / 1000)
for i in range(0, len(audio_bytes), frame_size * 2):
frame = audio_bytes[i:i + frame_size * 2]
if len(frame) == frame_size * 2 and vad.is_speech(frame, sample_rate):
return True
return False
@app.post("/submit_audio")
async def submit_audio(file: UploadFile = File(...)):
if file.content_type in ["audio/wav", "audio/mp3"]:
try:
contents = await file.read()
audio_io = io.BytesIO(contents)
audio_data, sr = sf.read(audio_io)
if is_speech(audio_data, sr):
return JSONResponse(status_code=200, content={"message": "Pembicaraan terdeteksi, bukan noise"})
noise_status = is_noisy(audio_data, NOISE_THRESHOLD)
return JSONResponse(status_code=200, content={"message": "Pindah ke tempat yang lebih tenang" if noise_status else "Tempat sudah kondusif!"})
except Exception as e:
return JSONResponse(status_code=500, content={"message": f"Terjadi kesalahan saat memproses file audio. Error:\n{str(e)}"})
else:
return JSONResponse(status_code=400, content={"message": "Format file tidak didukung"})
#############################################################################################################
if __name__ == "__main__":
uvicorn.run(app, host="127.0.0.1", port=8001, log_level="info")