schneegriesel
Demo Version, 1 sentence
4ec7d16
import spacy
import pandas as pd
from transformers import pipeline
import gradio as gr
import subprocess
import sys
# Versuche, spaCy zu laden – falls nicht vorhanden, lade es herunter
try:
nlp = spacy.load("de_core_news_sm")
except OSError:
subprocess.run([sys.executable, "-m", "spacy", "download", "de_core_news_sm"])
nlp = spacy.load("de_core_news_sm")
# Lade Sentimentmodell, cache lokal
sentiment_analyzer = pipeline(
"sentiment-analysis",
model="oliverguhr/german-sentiment-bert"
)
def link_entities_with_sentiment(text):
doc = nlp(text)
sentences = list(doc.sents)
entity_sentiment_links = []
for i, sentence in enumerate(sentences):
entities = [(ent.text, ent.label_) for ent in sentence.ents]
sentiment = sentiment_analyzer(sentence.text)[0]
for ent_text, ent_label in entities:
entity_sentiment_links.append({
"Entity": ent_text,
"Label": ent_label,
"Sentence Index": i,
"Sentiment Label": sentiment["label"],
"Sentiment Score": round(sentiment["score"], 3)
})
df = pd.DataFrame(entity_sentiment_links)
return df if not df.empty else "Keine Entitäten gefunden."
# Gradio Interface
demo = gr.Interface(
fn=link_entities_with_sentiment,
inputs=gr.Textbox(lines=10, label="Gib deinen deutschen Text ein"),
outputs=gr.Dataframe(label="Entitäten mit Sentiment"),
title="NER + Sentiment Analyse (Deutsch)",
description="Diese Demo verknüpft erkannte Entitäten mit Sentiment-Labels aus dem gleichen Satz.",
allow_flagging="manual",
)
if __name__ == "__main__":
demo.launch()