|
|
import { Injectable } from '@angular/core'; |
|
|
import { HttpClient, HttpHeaders } from '@angular/common/http'; |
|
|
import { Observable } from 'rxjs'; |
|
|
|
|
|
type Grade = 'lowergrade' | 'midgrade' | 'highergrade'; |
|
|
type DbLevel = 'low' | 'mid' | 'high'; |
|
|
|
|
|
function resolveBaseUrl(): string { |
|
|
const isHF = location.hostname.endsWith('hf.space'); |
|
|
if (isHF) return 'https://pykara-py-learn-backend.hf.space/rag'; |
|
|
if (location.hostname === 'localhost' || location.hostname === '127.0.0.1') { |
|
|
return 'http://localhost:5000/rag'; |
|
|
} |
|
|
return '/rag'; |
|
|
} |
|
|
|
|
|
@Injectable({ providedIn: 'root' }) |
|
|
export class ApiService { |
|
|
private baseUrl = resolveBaseUrl(); |
|
|
|
|
|
private getGrade(): Grade { |
|
|
const g = (localStorage.getItem('gradeLevel') || 'highergrade').toLowerCase(); |
|
|
if (g === 'lowergrade' || g === 'midgrade' || g === 'highergrade') return g as Grade; |
|
|
return 'highergrade'; |
|
|
} |
|
|
private toDbLevel(g: Grade): DbLevel { |
|
|
return g === 'lowergrade' ? 'low' : g === 'midgrade' ? 'mid' : 'high'; |
|
|
} |
|
|
private makeHeaders(g: Grade): HttpHeaders { |
|
|
return new HttpHeaders({ |
|
|
'Content-Type': 'application/json', |
|
|
'X-User': g, |
|
|
}); |
|
|
} |
|
|
|
|
|
constructor(private http: HttpClient) { } |
|
|
|
|
|
generateOpenQuestions(payload: { qtype: 'OPEN'; n?: number; topic?: string }): Observable<any> { |
|
|
const grade = this.getGrade(); |
|
|
const headers = this.makeHeaders(grade); |
|
|
const body = { |
|
|
qtype: 'OPEN', |
|
|
n: payload.n ?? 5, |
|
|
topic: payload.topic ?? '', |
|
|
model: 'gpt-4o-mini', |
|
|
db_level: this.toDbLevel(grade), |
|
|
}; |
|
|
return this.http.post<any>(`${this.baseUrl}/generate-questions`, body, { headers }); |
|
|
} |
|
|
|
|
|
|
|
|
explainGrammar(payload: string | any): Observable<any> { |
|
|
const grade = this.getGrade(); |
|
|
const headers = this.makeHeaders(grade); |
|
|
|
|
|
|
|
|
const body = typeof payload === 'string' ? { question: payload } : { ...payload }; |
|
|
|
|
|
|
|
|
if (!body.db_level) body.db_level = this.toDbLevel(grade); |
|
|
if (!body.model) body.model = 'gpt-4o-mini'; |
|
|
|
|
|
const url = `${this.baseUrl}/explain-grammar`; |
|
|
return this.http.post(url, body, { headers }); |
|
|
} |
|
|
|
|
|
|
|
|
suggestFollowups(payload: { |
|
|
last_question: string; |
|
|
last_answer: string; |
|
|
n?: number; |
|
|
source_ids?: string[]; |
|
|
}): Observable<any> { |
|
|
const grade = this.getGrade(); |
|
|
const headers = this.makeHeaders(grade); |
|
|
const body = { |
|
|
last_question: payload.last_question, |
|
|
last_answer: payload.last_answer, |
|
|
n: payload.n ?? 5, |
|
|
model: 'gpt-4o-mini', |
|
|
db_level: this.toDbLevel(grade), |
|
|
source_ids: payload.source_ids ?? [], |
|
|
}; |
|
|
return this.http.post<any>(`${this.baseUrl}/suggest-followups`, body, { headers }); |
|
|
} |
|
|
|
|
|
askQuestion(userInput: string, _sessionId: string | null): Observable<any> { |
|
|
return this.explainGrammar(userInput); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
synthesizeAudio(text: string, language = 'en', referenceFiles?: string[]) { |
|
|
const payload: any = { text, language }; |
|
|
if (referenceFiles) payload.reference_files = referenceFiles; |
|
|
return this.http.post<{ audio_url: string }>(`${this.baseUrl}/synthesize-audio`, payload); |
|
|
} |
|
|
|
|
|
|
|
|
synthesizeVideo(text: string, language = 'en') { |
|
|
const payload: any = { text, language }; |
|
|
return this.http.post<{ video_url: string }>(`${this.baseUrl}/synthesize-video`, payload); |
|
|
} |
|
|
|
|
|
punctuate(text: string): Observable<any> { |
|
|
const headers = { |
|
|
'Content-Type': 'application/json', |
|
|
'X-User': localStorage.getItem('username') || '' |
|
|
}; |
|
|
|
|
|
return this.http.post<any>(`${this.baseUrl}/punctuate`, { text }, { headers }); |
|
|
} |
|
|
} |
|
|
|