File size: 4,602 Bytes
2643dca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a756d31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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)) # منفذ مختلف عن سيرفر الدردشة