diff --git a/service/src/main/inse/config.py b/service/src/main/inse/config.py index 0b0fbe1f..578d830a 100644 --- a/service/src/main/inse/config.py +++ b/service/src/main/inse/config.py @@ -24,6 +24,10 @@ class Settings(BaseSettings): openalex_works_search_index: str = 'openalex-works' openalex_works_api_key: str = 'am1wWXBaMEJEVWJoZkE5MnBaUlk6TWlvbW1LRlMzNjdDUHgzYmVkUmI5UQ==' openalex_works_search_timeout_seconds: float = 30.0 + llm_api_url: str = 'https://project.nes.uni-due.de/llm2/api/generate' + llm_api_token: str = 'c2d5e7ccf7a4760dd3d19bed2caed9d85ff7437400e0ea82a6d6f912ebc2f212' + llm_model: str = 'gemma4:e4b' + elasticsearch_url: str = 'https://project.nes.uni-due.de/elastic/openalex-authors/_search' settings = Settings() logger.info(f"Database URL: {settings.database_url}.") diff --git a/service/src/main/inse/logic.py b/service/src/main/inse/logic.py index 708bd72d..e7e9c7b6 100644 --- a/service/src/main/inse/logic.py +++ b/service/src/main/inse/logic.py @@ -15,8 +15,11 @@ from sqlalchemy import insert from sqlalchemy import update from sqlalchemy import func import logging +import httpx +import json from . import schema from . import model +from . import config logger = logging.getLogger(__name__) @@ -55,3 +58,86 @@ def insert_element(db: Session, value: S) -> S: db.flush() db.refresh(value) return value + +def search_persons(author_id: Optional[str] = None, query: Optional[str] = None) -> model.PersonSearchResult: + headers = { + "Authorization": f"ApiKey {config.settings.elasticsearch_api_key}", + "Content-Type": "application/json" + } + + if author_id: + query_field = "_id" + query_value = author_id + elif query: + query_field = "display_name" + query_value = query + else: + return "Either author_id or query must be provided" + + payload = { + "query": { + "query_string": { + "default_field": query_field, + "query": query_value + } + } + } + + with httpx.Client() as client: + response = client.post(config.settings.elasticsearch_url, json=payload, headers=headers) + response.raise_for_status() + data = response.json() + + persons = [] + for hit in data.get("hits", {}).get("hits", []): + source = hit.get("_source", {}) + + affiliations = [] + for aff in source.get("affiliations", []): + institution = aff.get("institution", {}) + affiliations.append(model.Affiliation( + institution=institution.get("display_name", ""), + country_code=institution.get("country_code", "") + )) + + topics = [t.get("display_name", "") for t in source.get("topics", [])] + + persons.append(model.Person( + id=hit.get("_id", ""), + name=source.get("display_name", ""), + works_count=source.get("works_count", 0), + cited_by_count=source.get("cited_by_count", 0), + affiliations=affiliations, + topics=topics + )) + + return model.PersonSearchResult(persons=persons) + +def generate_person_bio(author_id: str) -> str: + search_result = search_persons(author_id=author_id) + if not search_result.persons: + return "No person found." + if len(search_result.persons) > 1: + return "Multiple persons found, cannot generate bio." + + person = search_result.persons[0] + person_json = json.dumps(person.model_dump()) + + system_prompt = "Erstelle eine Beschreibung der Person in 3-5 Sätzen. Gebe nur die Beschreibung zurück, ohne Einleitung oder Schluss." + user_prompt = f"Daten zur Person:\n{person_json}" + + headers = { + "Authorization": f"Bearer {config.settings.llm_api_token}", + "Content-Type": "application/json" + } + payload = { + "model": config.settings.llm_model, + "prompt": f"{system_prompt}\n{user_prompt}", + "stream": False + } + + with httpx.Client(timeout=30.0) as client: + response = client.post(config.settings.llm_api_url, json=payload, headers=headers) + response.raise_for_status() + data = response.json() + return data.get("response", "") diff --git a/service/src/main/inse/model.py b/service/src/main/inse/model.py index 7fe3e4d1..d3d7cf8c 100644 --- a/service/src/main/inse/model.py +++ b/service/src/main/inse/model.py @@ -10,6 +10,7 @@ from pydantic import Field from typing import List from typing import TypeVar from typing import Generic +from typing import Optional T = TypeVar('T', bound=BaseModel) @@ -34,3 +35,21 @@ class Page(BaseModel, Generic[T]): class ItemPage(Page[Item]): pass + +class PersonBio(BaseModel): + bio: str = Field(description="The generated biography.") + +class Affiliation(BaseModel): + institution: str = Field(description="Institution name.") + country_code: Optional[str] = Field(default=None, description="Country code.") + +class Person(BaseModel): + id: str = Field(description="The person id.") + name: str = Field(description="The person name.") + works_count: int = Field(description="Number of works.") + cited_by_count: int = Field(description="Number of citations.") + affiliations: List[Affiliation] = Field(default=[], description="Affiliations.") + topics: List[str] = Field(default=[], description="Research topics.") + +class PersonSearchResult(BaseModel): + persons: List[Person] = Field(default=[], description="List of found persons.") diff --git a/service/src/main/inse/service.py b/service/src/main/inse/service.py index a6d5ad4d..f47989b7 100644 --- a/service/src/main/inse/service.py +++ b/service/src/main/inse/service.py @@ -114,3 +114,21 @@ def delete_item(id: Annotated[int, Path(description='The item id.', ge=0)], return Response(status_code=HTTP_200_OK) finally: db.commit() + +@app.get('/persons/search', + operation_id='searchPersons', + response_model=model.PersonSearchResult, + summary='Searches for persons by ID or name.', + tags=[ 'Persons' ]) +def search_persons(author_id: Annotated[str, Query(description="OpenAlex author ID (e.g., A5093075883)")] = None, + query: Annotated[str, Query(description="Search query (e.g., Arman AND Arzani)")] = None): + return logic.search_persons(author_id, query) + +@app.get('/persons/bio', + operation_id='generatePersonBio', + response_model=model.PersonBio, + summary='Generates a bio for a person by author ID.', + tags=[ 'Persons' ]) +def generate_person_bio(author_id: Annotated[str, Query(description="OpenAlex author ID (e.g., A5093075883)")]): + bio = logic.generate_person_bio(author_id) + return model.PersonBio(bio=bio) diff --git a/webapp/src/app/app-layout/app-layout.component.scss b/webapp/src/app/app-layout/app-layout.component.scss index e4911619..a95d6680 100644 --- a/webapp/src/app/app-layout/app-layout.component.scss +++ b/webapp/src/app/app-layout/app-layout.component.scss @@ -18,7 +18,7 @@ .container { height: calc(100svh - 84px); overflow: auto; - background-color: #f4f6fb; + background-color: #ffffff; } mat-toolbar.main { @@ -43,18 +43,6 @@ mat-toolbar.main { background: linear-gradient(135deg, #1a3a6e 0%, #2259a8 100%); } -@media (prefers-color-scheme: dark) { - .container { - background-color: #1b1b1f; - } - mat-toolbar.main { - background: linear-gradient(135deg, #001849 0%, #002d6e 60%, #004290 100%); - } - .footer-container { - background: linear-gradient(135deg, #001849 0%, #002d6e 100%); - } -} - @media (max-width: 600px) { .large { display: none; @@ -66,4 +54,3 @@ mat-toolbar.main { height: calc(100svh - 76px); } } - diff --git a/webapp/src/app/app-login/app-login.component.scss b/webapp/src/app/app-login/app-login.component.scss index b1b7dd0b..f280c52c 100644 --- a/webapp/src/app/app-login/app-login.component.scss +++ b/webapp/src/app/app-login/app-login.component.scss @@ -50,20 +50,3 @@ .error { color: #ba1a1a; } - -@media (prefers-color-scheme: dark) { - .container { - background: linear-gradient(160deg, #1b1b1f 0%, #252530 100%); - } - .form { - background: #303034; - box-shadow: 0 2px 12px rgba(0,0,0,0.3); - } - .form h3 { - color: #a4c4ff; - } - .progress { - color: #c1c8dc; - } -} - diff --git a/webapp/src/app/app-menu/app-menu.component.scss b/webapp/src/app/app-menu/app-menu.component.scss index 695ac76c..0796cbf0 100644 --- a/webapp/src/app/app-menu/app-menu.component.scss +++ b/webapp/src/app/app-menu/app-menu.component.scss @@ -5,9 +5,3 @@ background-color: mat.get-theme-color($light-theme, primary-container); border-radius: 8px; } - -@media (prefers-color-scheme: dark) { - .active { - background-color: mat.get-theme-color($dark-theme, primary-container); - } -} \ No newline at end of file diff --git a/webapp/src/app/app.routes.ts b/webapp/src/app/app.routes.ts index d9295c47..e69c57c9 100644 --- a/webapp/src/app/app.routes.ts +++ b/webapp/src/app/app.routes.ts @@ -42,6 +42,13 @@ export const routes: Routes = [ (mod) => mod.PersonNetworkComponent, ), }, + { + path: 'publications', + loadComponent: () => + import('./person-publications/person-publications.component').then( + (mod) => mod.PersonPublicationsComponent, + ), + }, ], }, { diff --git a/webapp/src/app/list-item/list-item.component.scss b/webapp/src/app/list-item/list-item.component.scss index cb74b364..e061b798 100644 --- a/webapp/src/app/list-item/list-item.component.scss +++ b/webapp/src/app/list-item/list-item.component.scss @@ -50,19 +50,6 @@ mat-paginator { border-radius: 0 0 12px 12px; } -@media (prefers-color-scheme: dark) { - .form-section { - background: #303034; - box-shadow: 0 1px 6px rgba(0,0,0,0.2); - } - .form-section h3 { - color: #a4c4ff; - } - ::ng-deep .mat-mdc-header-row { - background-color: #1b1b22; - } -} - @media (max-width: 600px) { .page { padding: 16px; diff --git a/webapp/src/app/person-publications/person-publications.component.html b/webapp/src/app/person-publications/person-publications.component.html new file mode 100644 index 00000000..a4a48060 --- /dev/null +++ b/webapp/src/app/person-publications/person-publications.component.html @@ -0,0 +1,86 @@ +
+

Publikationen

+ +
+ @if (loading) { +
+ +
+ } + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Titel +
{{ publication.title || 'Ohne Titel' }}
+
Jahr{{ publication.publication_year || '-' }}Typ + @if (publication.type) { + {{ publication.type }} + } @else { + - + } + Sprache{{ publication.language || '-' }}Zitationen{{ publication.cited_by_count }}Themen + + @for (topic of publication.topics.slice(0, 3); track topic) { + {{ topic }} + } @empty { + - + } + + DOI + @if (doiUrl(publication.doi); as url) { + + open_in_new + + } @else { + - + } +
+ + @if (!loading && publications.length === 0) { +
Keine Publikationen gefunden.
+ } +
+ + + +
diff --git a/webapp/src/app/person-publications/person-publications.component.scss b/webapp/src/app/person-publications/person-publications.component.scss new file mode 100644 index 00000000..1d06aa96 --- /dev/null +++ b/webapp/src/app/person-publications/person-publications.component.scss @@ -0,0 +1,80 @@ +.page { + padding: 28px 32px; + max-width: 1200px; + margin: 0 auto; +} + +h2 { + margin: 0 0 20px; + font-weight: 600; +} + +.table-wrap { + position: relative; + overflow-x: auto; +} + +table { + width: 100%; + min-width: 920px; + border-radius: 8px; + overflow: hidden; +} + +.title { + max-width: 420px; + line-height: 1.35; + font-weight: 600; +} + +.loading { + position: absolute; + inset: 0; + z-index: 1; + display: grid; + place-items: center; + background: rgba(255, 255, 255, 0.65); +} + +.empty-state { + padding: 28px; + text-align: center; + color: rgba(0, 0, 0, 0.6); + background: white; +} + +.empty-text { + color: rgba(0, 0, 0, 0.54); +} + +mat-chip-set { + display: flex; + max-width: 320px; +} + +mat-chip, +.type-chip { + --mdc-chip-elevated-container-color: #e3f2fd; + --mdc-chip-label-text-color: #0d47a1; + --mdc-chip-outline-color: #bbdefb; + max-width: 140px; +} + +a { + display: inline-flex; + color: inherit; +} + +::ng-deep .mat-mdc-header-row { + background-color: #eaf0ff; +} + +mat-paginator { + margin-top: 8px; +} + +@media (max-width: 600px) { + .page { + padding: 16px; + } +} diff --git a/webapp/src/app/person-publications/person-publications.component.ts b/webapp/src/app/person-publications/person-publications.component.ts new file mode 100644 index 00000000..948483a4 --- /dev/null +++ b/webapp/src/app/person-publications/person-publications.component.ts @@ -0,0 +1,74 @@ +import { Component, OnInit } from '@angular/core'; +import { ActivatedRoute } from '@angular/router'; +import { MatChipsModule } from '@angular/material/chips'; +import { MatIconModule } from '@angular/material/icon'; +import { MatPaginatorModule, PageEvent } from '@angular/material/paginator'; +import { MatProgressSpinnerModule } from '@angular/material/progress-spinner'; +import { MatSnackBar, MatSnackBarModule } from '@angular/material/snack-bar'; +import { MatTableModule } from '@angular/material/table'; + +import { PaginationService } from '../services/pagination.service'; +import { PublicationsService, WorkSummary } from '../services/publications.service'; +import { createLoadError } from '../utils/errorHandler'; + +@Component({ + selector: 'app-person-publications', + imports: [ + MatChipsModule, + MatIconModule, + MatPaginatorModule, + MatProgressSpinnerModule, + MatSnackBarModule, + MatTableModule, + ], + templateUrl: './person-publications.component.html', + styleUrl: './person-publications.component.scss', +}) +export class PersonPublicationsComponent implements OnInit { + displayedColumns: string[] = ['title', 'year', 'type', 'language', 'citations', 'topics', 'doi']; + publications: WorkSummary[] = []; + total = 0; + loading = false; + authorId = ''; + + constructor( + private route: ActivatedRoute, + private publicationsService: PublicationsService, + public paginationService: PaginationService, + private snackBar: MatSnackBar, + ) {} + + ngOnInit(): void { + this.route.parent?.paramMap.subscribe((params) => { + this.authorId = params.get('id') ?? ''; + this.load(); + }); + } + + load(pageIndex = 0, pageSize = this.paginationService.selection): void { + if (!this.authorId) return; + + this.loading = true; + this.publicationsService.getAuthorWorks(this.authorId, pageIndex + 1, pageSize).subscribe({ + next: (page) => { + this.publications = page.elements; + this.total = page.total; + this.loading = false; + }, + error: (error) => { + this.loading = false; + createLoadError(this.snackBar, () => this.load(pageIndex, pageSize), 'Publikationen konnten nicht geladen werden')(error); + }, + }); + } + + onPage(event: PageEvent): void { + this.paginationService.selection = event.pageSize; + this.load(event.pageIndex, event.pageSize); + } + + doiUrl(doi: string | null): string | null { + if (!doi) return null; + return doi.startsWith('http') ? doi : `https://doi.org/${doi}`; + } +} diff --git a/webapp/src/app/person/person.component.html b/webapp/src/app/person/person.component.html index d8f8c797..e801c2c8 100644 --- a/webapp/src/app/person/person.component.html +++ b/webapp/src/app/person/person.component.html @@ -1,18 +1,16 @@ -

person works!

- - - + + + diff --git a/webapp/src/app/person/person.component.ts b/webapp/src/app/person/person.component.ts index 5d44472c..34050995 100644 --- a/webapp/src/app/person/person.component.ts +++ b/webapp/src/app/person/person.component.ts @@ -12,7 +12,7 @@ export class PersonComponent { links = [ { label: 'Übersicht', path: 'overview', disabled: false }, { label: 'Projekte', path: 'projects', disabled: true }, - { label: 'Publikationen', path: 'publications', disabled: true }, + { label: 'Publikationen', path: 'publications', disabled: false }, { label: 'Netzwerk', path: 'network', disabled: false }, { label: 'Gründungen', path: 'foundings', disabled: true }, ]; diff --git a/webapp/src/app/services/publications.service.ts b/webapp/src/app/services/publications.service.ts new file mode 100644 index 00000000..4ea0ab7c --- /dev/null +++ b/webapp/src/app/services/publications.service.ts @@ -0,0 +1,43 @@ +import { Injectable } from '@angular/core'; +import { HttpClient, HttpParams } from '@angular/common/http'; +import { Observable } from 'rxjs'; + +export interface WorkSummary { + doi: string | null; + title: string | null; + type: string | null; + language: string | null; + publication_year: number | null; + cited_by_count: number; + topics: string[]; +} + +export interface WorkPage { + elements: WorkSummary[]; + page: number; + total: number; + size: number; +} + +@Injectable({ + providedIn: 'root', +}) +export class PublicationsService { + private readonly basePath = '/inse-service'; + + constructor(private http: HttpClient) {} + + getAuthorWorks( + authorId: string, + page: number = 1, + size: number = 10, + ): Observable { + const params = new HttpParams() + .set('page', page.toString()) + .set('size', size.toString()); + return this.http.get( + `${this.basePath}/authors/${authorId}/works`, + { params }, + ); + } +} diff --git a/webapp/src/styles.scss b/webapp/src/styles.scss index 2e7e0441..1ed4bdd5 100644 --- a/webapp/src/styles.scss +++ b/webapp/src/styles.scss @@ -60,12 +60,14 @@ html { padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left); height: 100%; + background: #ffffff; @include mat.all-component-themes(m3.$light-theme); @include mat.typography-hierarchy(m3.$light-theme); } body { height: 100%; margin: 0; + background: #ffffff; } @@ -90,33 +92,7 @@ body { } -@media (prefers-color-scheme: dark) { - html { - @include mat.all-component-colors(m3.$dark-theme); - } - .primary-button { - @include mat.button-color(m3.$dark-theme, $color-variant: primary); - } - .secondary-button { - @include mat.button-color(m3.$dark-theme, $color-variant: secondary); - } - .tertiary-button { - @include mat.button-color(m3.$dark-theme, $color-variant: tertiary); - } - .primary-icon { - @include mat.icon-color(m3.$dark-theme, $color-variant: primary); - } - .secondary-icon { - @include mat.icon-color(m3.$dark-theme, $color-variant: secondary); - } - .tertiary-icon { - @include mat.icon-color(m3.$dark-theme, $color-variant: tertiary); - } -} - .menu button { --mat-icon-button-disabled-icon-color: rgba(255,255,255,0.35); --mat-icon-button-icon-color: #ffffff; } - -