|  | from fastapi import FastAPI, Request | 
					
						
						|  | from fastapi.responses import HTMLResponse | 
					
						
						|  | from fastapi.staticfiles import StaticFiles | 
					
						
						|  | from fastapi.templating import Jinja2Templates | 
					
						
						|  | import os | 
					
						
						|  | from huggingface_hub import InferenceClient | 
					
						
						|  |  | 
					
						
						|  | app = FastAPI() | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | app.mount("/static", StaticFiles(directory="static"), name="static") | 
					
						
						|  | templates = Jinja2Templates(directory="templates") | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | HF_TOKEN = os.environ.get("HF_TOKEN", "") | 
					
						
						|  |  | 
					
						
						|  | @app.get("/", response_class=HTMLResponse) | 
					
						
						|  | async def root(request: Request): | 
					
						
						|  | return templates.TemplateResponse("index.html", {"request": request}) | 
					
						
						|  |  | 
					
						
						|  | @app.post("/analyze") | 
					
						
						|  | async def analyze_text(request: Request): | 
					
						
						|  | data = await request.json() | 
					
						
						|  | user_text = data.get("text", "") | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | client = InferenceClient(token=HF_TOKEN) | 
					
						
						|  | sentiment = client.text_classification( | 
					
						
						|  | text=user_text, | 
					
						
						|  | model="cardiffnlp/twitter-roberta-base-sentiment" | 
					
						
						|  | ) | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | messages = data.get("history", []) | 
					
						
						|  | messages.append({"role": "user", "content": user_text}) | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | best_sentiment = sorted(sentiment, key=lambda x: x["score"], reverse=True)[0] | 
					
						
						|  | if best_sentiment["label"] == "NEGATIVE" and best_sentiment["score"] > 0.6: | 
					
						
						|  | messages.append({"role": "system", "content": "Der Patient zeigt starke negative Emotionen – schlage Schuldprojektion oder Verdrängung vor."}) | 
					
						
						|  | elif best_sentiment["label"] == "POSITIVE" and best_sentiment["score"] > 0.6: | 
					
						
						|  | messages.append({"role": "system", "content": "Der Patient wirkt übertrieben positiv – möglicherweise Abwehrmechanismus durch Kompensation."}) | 
					
						
						|  |  | 
					
						
						|  |  | 
					
						
						|  | response = client.chat_completion( | 
					
						
						|  | model="gpt-3.5-turbo", | 
					
						
						|  | messages=messages | 
					
						
						|  | ) | 
					
						
						|  |  | 
					
						
						|  | return { | 
					
						
						|  | "reply": response.generated_text, | 
					
						
						|  | "toneLabel": best_sentiment["label"], | 
					
						
						|  | "toneScore": best_sentiment["score"] | 
					
						
						|  | } |