File size: 4,154 Bytes
f275565
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9274cc0
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
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  # النص المجمع من حوار المستخدم فقط

@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 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)) # منفذ مختلف عن سيرفر الدردشة