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 @@ +
| Titel | +
+ {{ publication.title || 'Ohne Titel' }}
+ |
+ Jahr | +{{ publication.publication_year || '-' }} | +Typ | +
+ @if (publication.type) {
+ |
+ Sprache | +{{ publication.language || '-' }} | +Zitationen | +{{ publication.cited_by_count }} | +Themen | +
+ |
+ DOI | +
+ @if (doiUrl(publication.doi); as url) {
+
+ |
+
|---|
person works!
-