Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,11 +10,9 @@ import requests
|
|
| 10 |
app = Flask(__name__)
|
| 11 |
CORS(app)
|
| 12 |
|
| 13 |
-
# --- Configuration for the Language Model (
|
| 14 |
-
LLM_API_URL = "https://api-inference.huggingface.co/
|
| 15 |
-
|
| 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
|
| 52 |
if not HF_TOKEN:
|
| 53 |
return jsonify({"error": "Hugging Face API token is not configured on the server."}), 500
|
| 54 |
|
| 55 |
-
|
|
|
|
|
|
|
| 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 |
-
# ---
|
| 64 |
payload = {
|
| 65 |
-
"
|
| 66 |
-
"
|
| 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 |
-
# ---
|
| 76 |
-
generated_text = llm_result[
|
| 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"],
|