Commit ff160c15 authored by Gert Paimla's avatar Gert Paimla

article generation

parent 19557662
......@@ -3,45 +3,52 @@
<form #generatorForm="ngForm" (ngSubmit)="submitForm()" class="mw" nz-form nzLayout="vertical">
<nz-form-item>
<nz-form-control nzErrorTip="This field is required">
<nz-form-label nzRequired>Dataset</nz-form-label>
<nz-form-label nzRequired>Language</nz-form-label>
<nz-select
[(ngModel)]="selectedDataset"
(ngModelChange)="languageSelected.next($event)"
[(ngModel)]="selectedLanguage"
[nzLoading]="!languages"
class="mw"
name="selectedDataset"
name="selectedLanguage"
required>
<nz-option *ngFor="let item of dataset" [nzLabel]="item.display_name"
[nzValue]="item.value"></nz-option>
<nz-option *ngFor="let item of languages?.languages" [nzLabel]="item"
[nzValue]="item"></nz-option>
</nz-select>
</nz-form-control>
</nz-form-item>
<nz-form-item>
<nz-form-control nzErrorTip="This field is required">
<nz-form-label nzRequired>Location</nz-form-label>
<nz-form-label nzRequired>Dataset</nz-form-label>
<nz-select
[(ngModel)]="selectedLocation"
(ngModelChange)="datasetSelected.next($event)"
[(ngModel)]="selectedDataset"
[nzLoading]="!datasets"
class="mw"
name="selectedLocation"
name="selectedDataset"
[disabled]="!selectedLanguage"
required>
<nz-option *ngFor="let item of location" [nzLabel]="item.display_name"
[nzValue]="item.value"></nz-option>
<nz-option *ngFor="let item of datasets?.datasets" [nzLabel]="item"
[nzValue]="item"></nz-option>
</nz-select>
</nz-form-control>
</nz-form-item>
<nz-form-item>
<nz-form-control nzErrorTip="This field is required">
<nz-form-label nzRequired>Language</nz-form-label>
<nz-form-label nzRequired>Location</nz-form-label>
<nz-select
[(ngModel)]="selectedLanguage"
[(ngModel)]="selectedLocation"
[disabled]="!selectedDataset"
[nzLoading]="!locations"
class="mw"
name="selectedLanguage"
name="selectedLocation"
required>
<nz-option *ngFor="let item of language" [nzLabel]="item.display_name"
[nzValue]="item.value"></nz-option>
<nz-option *ngFor="let item of locations?.locations" [nzLabel]="item"
[nzValue]="item"></nz-option>
</nz-select>
</nz-form-control>
......@@ -55,7 +62,7 @@
</div>
<div class="content">
<nz-spin *ngIf="isLoading" [nzSize]="'large'" [nzSpinning]="isLoading" nzSimple></nz-spin>
<div class="container" *ngIf="this.results">
<div *ngIf="this.results" class="container">
<h1 class="mw">{{this.results.header}}</h1>
<div [innerHTML]="this.results.body">
</div>
......
......@@ -2,10 +2,10 @@ import {Component, OnDestroy, OnInit} from '@angular/core';
import {GeneratorsService} from '../core/generators.service';
import {HttpErrorResponse} from '@angular/common/http';
import {LogService} from '../core/log.service';
import {Choice} from '../shared/types/GeneratorOptions';
import {Datasets, Languages, Locations} from '../shared/types/GeneratorOptions';
import {GeneratorResponse} from '../shared/types/GeneratorResponse';
import {Subject} from 'rxjs';
import {takeUntil} from 'rxjs/operators';
import {switchMap, takeUntil} from 'rxjs/operators';
@Component({
selector: 'app-article-generation',
......@@ -15,28 +15,45 @@ import {takeUntil} from 'rxjs/operators';
export class ArticleGenerationComponent implements OnInit, OnDestroy {
isLoading = false;
dataset: Choice[] = [];
location: Choice[] = [];
language: Choice[] = [];
datasets: Datasets | null;
locations: Locations | null;
languages: Languages | null;
selectedLocation: string;
selectedDataset: string;
selectedLanguage: string;
results: GeneratorResponse | null;
destroyed$: Subject<boolean> = new Subject<boolean>();
languageSelected: Subject<'fi' | 'hr' | 'en'> = new Subject<'fi' | 'hr' | 'en'>();
datasetSelected: Subject<string> = new Subject<string>();
constructor(private generatorsService: GeneratorsService, private logService: LogService) {
}
ngOnInit(): void {
this.generatorsService.getGeneratorsOptions().pipe(takeUntil(this.destroyed$)).subscribe(x => {
this.generatorsService.getLanguages().pipe(takeUntil(this.destroyed$)).subscribe(x => {
if (x && !(x instanceof HttpErrorResponse)) {
this.dataset = x.actions.POST.dataset.choices;
this.selectedDataset = this.dataset[0].value;
this.location = x.actions.POST.location.choices;
this.selectedLocation = this.location[0].value;
this.language = x.actions.POST.language.choices;
this.selectedLanguage = this.language[0].value;
} else if (x instanceof HttpErrorResponse) {
this.languages = x;
} else {
this.logService.messageHttpError(x);
}
});
this.languageSelected.pipe(takeUntil(this.destroyed$), switchMap(x => {
this.datasets = null;
return this.generatorsService.getDatasets(x);
})).subscribe(x => {
if (x && !(x instanceof HttpErrorResponse)) {
this.datasets = x;
} else {
this.logService.messageHttpError(x);
}
});
this.datasetSelected.pipe(takeUntil(this.destroyed$), switchMap(x => {
this.locations = null;
return this.generatorsService.getLocations(x);
})).subscribe(x => {
if (x && !(x instanceof HttpErrorResponse)) {
this.locations = x;
} else {
this.logService.messageHttpError(x);
}
});
......
......@@ -4,30 +4,46 @@ import {HttpClient, HttpErrorResponse} from '@angular/common/http';
import {LogService} from './log.service';
import {Observable} from 'rxjs';
import {catchError, tap} from 'rxjs/operators';
import {GeneratorOptions} from '../shared/types/GeneratorOptions';
import {GeneratorResponse} from '../shared/types/GeneratorResponse';
import {Datasets, Languages, Locations} from '../shared/types/GeneratorOptions';
@Injectable({
providedIn: 'root'
})
export class GeneratorsService {
apiUrl = environment.apiHost + environment.apiBasePath;
apiUrl = environment.apiNLG;
constructor(private http: HttpClient, private logService: LogService) {
}
getGeneratorsOptions(): Observable<GeneratorOptions | HttpErrorResponse> {
return this.http.options<GeneratorOptions>(
`${this.apiUrl}/article_generator/`
getLanguages(): Observable<Languages | HttpErrorResponse> {
return this.http.get<Languages>(
`${this.apiUrl}/languages/`
).pipe(
tap(e => this.logService.logStatus(e, 'getGeneratorsOptions')),
catchError(this.logService.handleError<GeneratorOptions>('getGeneratorsOptions')));
tap(e => this.logService.logStatus(e, 'getLanguages')),
catchError(this.logService.handleError<Languages>('getLanguages')));
}
getDatasets(language: 'fi' | 'en' | 'hr'): Observable<Datasets | HttpErrorResponse> {
return this.http.post<Datasets>(
`${this.apiUrl}/datasets/`, {language}
).pipe(
tap(e => this.logService.logStatus(e, 'getDatasets')),
catchError(this.logService.handleError<Datasets>('getDatasets')));
}
getLocations(dataset: string): Observable<Locations | HttpErrorResponse> {
return this.http.post<Locations>(
`${this.apiUrl}/locations/`, {dataset}
).pipe(
tap(e => this.logService.logStatus(e, 'getLocations')),
catchError(this.logService.handleError<Locations>('getLocations')));
}
generateText(body: any): Observable<GeneratorResponse | HttpErrorResponse> {
return this.http.post<GeneratorResponse>(
`${this.apiUrl}/article_generator/`, body
`${this.apiUrl}/eunlg/`, body
).pipe(
tap(e => this.logService.logStatus(e, 'generateText')),
catchError(this.logService.handleError<GeneratorResponse>('generateText')));
......
export interface Choice {
value: string;
display_name: string;
export interface Languages {
languages: 'fi' | 'hr' | 'en'[];
}
export interface Dataset {
type: string;
required: boolean;
read_only: boolean;
label: string;
choices: Choice[];
export interface Datasets {
datasets: string[];
}
export interface Choice2 {
value: string;
display_name: string;
}
export interface Location {
type: string;
required: boolean;
read_only: boolean;
label: string;
choices: Choice2[];
}
export interface Choice3 {
value: string;
display_name: string;
}
export interface Language {
type: string;
required: boolean;
read_only: boolean;
label: string;
choices: Choice3[];
}
export interface POST {
dataset: Dataset;
location: Location;
language: Language;
}
export interface Actions {
POST: POST;
}
export interface GeneratorOptions {
name: string;
description: string;
renders: string[];
parses: string[];
actions: Actions;
export interface Locations {
locations: string[];
}
export const environment = {
apiHost: 'REST_API_URL_REPLACE',
apiNLG: 'NLG_API_URL_REPLACE',
apiBasePath: '',
production: true
};
......@@ -5,6 +5,7 @@
export const environment = {
apiHost: 'http://dev.texta.ee:8090',
apiBasePath: '',
apiNLG: 'http://dev.texta.ee:5555',
production: false
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment