from fastapi import FastAPI, HTTPException from pydantic import BaseModel import httpx import json import os import uvicorn app = FastAPI() OLLAMA_URL = "http://localhost:11434/api/chat" # تعريف شكل البيانات المستلمة class AnalysisRequest(BaseModel): user_text: str # النص المجمع من حوار المستخدم فقط @app.get("/") async def check(): return { "status": "success", "state":"ok" } @app.post("/analyze/personality") async def analyze_personality(data: AnalysisRequest): # الـ Prompt باللغة الإنجليزية فقط # analysis_prompt = f""" # Analyze the following user text and return a JSON object with EXACTLY these English keys: # 'decision_making', 'energy', 'focus', 'lifestyle', 'compatible_type'. # Requirements: # 1. The 'summary' field inside each key must be written in ARABIC. # 2. The 'summary' should be a concise psychological insight (1-2 sentences). # 3. 'compatible_type' must be a 4-letter MBTI code in ENGLISH (e.g., 'INFJ'). # User Text: # \"\"\"{data.user_text}\"\"\" # Respond ONLY in valid JSON. # """ # analysis_prompt = f""" # Analyze the following user text based on MBTI personality theory. # Return a JSON object with EXACTLY these keys: # 'personality_type', 'decision_making', 'energy', 'focus', 'lifestyle', 'compatible_type'. # Requirements: # 1. 'personality_type': MUST be the 4-letter MBTI code that is the user's identified type. # 2. The 'summary' field inside 'decision_making', 'energy', 'focus', and 'lifestyle' must be written in ARABIC. # 3. The 'summary' should be a concise psychological insight (1-2 sentences) in ARABIC just in Arabic. # 4. 'compatible_type': The 4-letter MBTI code in ENGLISH that best matches the user. # User Text: # \"\"\"{data.user_text}\"\"\" # Respond ONLY in ARABIC # Respond ONLY in valid JSON. # """ # analysis_prompt = """ # Analyze the following text based on MBTI theory. # Return ONLY a JSON object with this EXACT structure: # {{ # "personality_type": "MUST be the 4-letter MBTI code that is the user's identified type. (e.g., 'INFJ')", # "decision_making": {{ "summary": "تحليل باللغة العربية هنا" }}, # "energy": {{ "summary": "تحليل باللغة العربية هنا" }}, # "focus": {{ "summary": "تحليل باللغة العربية هنا" }}, # "lifestyle": {{ "summary": "تحليل باللغة العربية هنا" }}, # "compatible_type": "MUST be the 4-letter MBTI code that is the user's identified type. (e.g., 'INFJ')" # }} # User Text:""" # analysis_prompt +=f""" # \"\"\"{data.user_text}\"\"\" # """ analysis_prompt = f""" أنت محلل شخصيات متخصص. حلل نص المستخدم واستخرج كود MBTI المكون من 4 أحرف فقط. المطلوب: 1. الرد بصيغة JSON فقط. 2. الكود يجب أن يكون بالإنجليزية (مثل: ENFP). النص المراد تحليله: {data.user_text} """ analysis_prompt +=""" قالب الرد: { "mbti": "MUST be the 4-letter MBTI code that is the user's identified type. (e.g., 'INFJ')" } """ # print(analysis_prompt) payload = { "model": "qwen2.5:1.5b",#"qwen2.5:3b",#qwen2.5:7b "messages": [ {"role": "system", "content": "أنت خبير في تحليل الشخصيات النفسية. يجب أن تكون إجابتك دائماً بصيغة JSON صالحة فقط. استخدم اللغة العربية في كتابة التحليلات."}, {"role": "user", "content": analysis_prompt} ], "options":{ "temperature":0.3, }, "stream": False, "format": "json" } async with httpx.AsyncClient(timeout=400.0) as client: try: url = os.environ.get("OLLAMA_URL", OLLAMA_URL) response = await client.post(url, json=payload) response.raise_for_status() result = response.json() analysis_content = result.get("message", {}).get("content", "") # print(json.loads(analysis_content)) return json.loads(analysis_content) except Exception as e: print(e) raise HTTPException(status_code=500, detail=str(e)) uvicorn.run(app, host="0.0.0.0", port=os.environ.get("PORT",7860)) # منفذ مختلف عن سيرفر الدردشة