Pro1222 commited on
Commit
35b1bb6
·
verified ·
1 Parent(s): 163fc70

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -16
app.py CHANGED
@@ -10,11 +10,9 @@ import requests
10
  app = Flask(__name__)
11
  CORS(app)
12
 
13
- # --- Configuration for the Language Model (Using the new Chat Completions endpoint) ---
14
- LLM_API_URL = "https://api-inference.huggingface.co/v1/chat/completions"
15
- MODEL_ID = "microsoft/phi-3-mini-4k-instruct"
16
- # The secret name is now HF_TOKEN to match the new standard
17
- HF_TOKEN = os.environ.get("HF_TOKEN")
18
 
19
  @app.route('/')
20
  def home():
@@ -48,32 +46,34 @@ def analyze():
48
  "emotion": face_data.get('dominant_emotion'),
49
  }
50
 
51
- # Part 2: Comment Generation via new Chat Completions API
52
  if not HF_TOKEN:
53
  return jsonify({"error": "Hugging Face API token is not configured on the server."}), 500
54
 
55
- prompt_content = f"""You are ReflectAI. A person's photo shows an expression of '{attributes['emotion']}', they are a {attributes['gender']} aged around {attributes['age']}.
 
 
56
  Write a two-part response:
57
  1. **Comment:** A short, insightful, and encouraging comment about their aura.
58
  2. **Advice:** A brief, actionable piece of advice related to their expression or age.
59
- Format the output with "Comment:" and "Advice:" labels. Do not repeat the attributes."""
 
 
60
 
61
  headers = {"Authorization": f"Bearer {HF_TOKEN}"}
62
 
63
- # --- New Payload Structure ---
64
  payload = {
65
- "model": MODEL_ID,
66
- "messages": [{"role": "user", "content": prompt_content}],
67
- "max_tokens": 120,
68
- "temperature": 0.8
69
  }
70
 
71
  llm_response = requests.post(LLM_API_URL, headers=headers, json=payload)
72
- llm_response.raise_for_status()
73
  llm_result = llm_response.json()
74
 
75
- # --- New Response Parsing ---
76
- generated_text = llm_result["choices"][0]["message"]["content"].strip()
77
 
78
  final_response = {
79
  "age": attributes["age"],
 
10
  app = Flask(__name__)
11
  CORS(app)
12
 
13
+ # --- Configuration for the Language Model (Reverting to the stable endpoint) ---
14
+ LLM_API_URL = "https://api-inference.huggingface.co/models/microsoft/phi-3-mini-4k-instruct"
15
+ HF_TOKEN = os.environ.get("HF_TOKEN") # The secret name remains the same
 
 
16
 
17
  @app.route('/')
18
  def home():
 
46
  "emotion": face_data.get('dominant_emotion'),
47
  }
48
 
49
+ # Part 2: Comment Generation
50
  if not HF_TOKEN:
51
  return jsonify({"error": "Hugging Face API token is not configured on the server."}), 500
52
 
53
+ # Create the prompt for the language model
54
+ prompt = f"""<|user|>
55
+ You are ReflectAI. A person's photo shows an expression of '{attributes['emotion']}', they are a {attributes['gender']} aged around {attributes['age']}.
56
  Write a two-part response:
57
  1. **Comment:** A short, insightful, and encouraging comment about their aura.
58
  2. **Advice:** A brief, actionable piece of advice related to their expression or age.
59
+ Format the output with "Comment:" and "Advice:" labels. Do not repeat the attributes.<|end|>
60
+ <|assistant|>
61
+ """
62
 
63
  headers = {"Authorization": f"Bearer {HF_TOKEN}"}
64
 
65
+ # --- Reverting to the simpler, more stable payload structure ---
66
  payload = {
67
+ "inputs": prompt,
68
+ "parameters": {"max_new_tokens": 120, "temperature": 0.7, "return_full_text": False}
 
 
69
  }
70
 
71
  llm_response = requests.post(LLM_API_URL, headers=headers, json=payload)
72
+ llm_response.raise_for_status() # Raise an exception for HTTP errors
73
  llm_result = llm_response.json()
74
 
75
+ # --- Reverting to the simpler response parsing ---
76
+ generated_text = llm_result[0]['generated_text'].strip()
77
 
78
  final_response = {
79
  "age": attributes["age"],