Spaces:
Running
Running
| from fastapi import FastAPI, HTTPException | |
| from pydantic import BaseModel | |
| import httpx | |
| import json | |
| import uvicorn | |
| import os | |
| app = FastAPI() | |
| OLLAMA_URL = "http://localhost:11434/api/chat" | |
| # تعريف شكل البيانات المستلمة | |
| class AnalysisRequest(BaseModel): | |
| user_text: str # النص المجمع من حوار المستخدم فقط | |
| async def check(): | |
| return { | |
| "status": "success", | |
| "state":"ok" | |
| } | |
| async def analyze_personality(data: AnalysisRequest): | |
| # الـ Prompt باللغة الإنجليزية فقط | |
| # analysis_prompt = f""" | |
| # Analyze the user text and return a JSON object with exactly these keys: | |
| # 'decision_making', 'energy', 'focus', 'lifestyle', 'compatible_type' 'personality_type' ,. | |
| # Each key (except compatible_type and personality_type) must be an object with a 'summary' field in ENGLISH. | |
| # The 'summary' should be a concise psychological insight (1-2 sentences). | |
| # 'compatible_type' should be a 4-letter MBTI code (e.g., 'INTJ') just it not a code inside it. | |
| # 'personality_type' should be a 4-letter MBTI code (e.g., 'INTJ') just it not a code inside it. | |
| # User Text: "{data.user_text}" | |
| # """ | |
| analysis_prompt = f""" | |
| Act as an MBTI classifier. Analyze the following text and identify the user's personality type. | |
| Constraints: | |
| 1. Output MUST be ONLY a valid JSON. | |
| 2. Provide ONLY the 4-letter MBTI code. | |
| User Text: | |
| {data.user_text} | |
| """ | |
| analysis_prompt += """ | |
| Response Format: | |
| { | |
| "mbti": "MUST be a 4-letter MBTI code (e.g., 'INTJ') just it not a code inside it." | |
| } | |
| """ | |
| # analysis_prompt = f""" | |
| # Analyze the user text based on MBTI personality theory. | |
| # Return a JSON object with EXACTLY these keys: | |
| # '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. There is a 'summary' field inside 'decision_making', 'energy', 'focus', and 'lifestyle' | |
| # 2. For each 'summary' field (except compatible_type), write a 1-2 sentence psychological insight in ENGLISH. | |
| # 3. 'compatible_type' MUST be the 4-letter MBTI code that is most compatible with the user's identified type. | |
| # User Text: "{data.user_text}" | |
| # """ | |
| # 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 English. | |
| # 3. The 'summary' should be a concise psychological insight (1-2 sentences) in English. | |
| # 4. 'compatible_type': The 4-letter MBTI code in ENGLISH that best matches the user. | |
| # User Text: | |
| # \"\"\"{data.user_text}\"\"\" | |
| # Respond ONLY in valid JSON. | |
| # """ | |
| payload = { | |
| "model": "llama3.2:1b", | |
| "messages": [ | |
| {"role": "system", "content": "You are a professional MBTI profiler. Respond ONLY in valid JSON. All text must be in English."}, | |
| {"role": "user", "content": analysis_prompt} | |
| ], | |
| "stream": False, | |
| "format": "json" | |
| } | |
| async with httpx.AsyncClient(timeout=60.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", "") | |
| return json.loads(analysis_content) | |
| except Exception as e: | |
| raise HTTPException(status_code=500, detail=str(e)) | |
| uvicorn.run(app, host="0.0.0.0", port=os.environ.get("PORT",7860)) # منفذ مختلف عن سيرفر الدردشة |