hrlima commited on
Commit
e621b89
verified
1 Parent(s): 2bcf2cc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -12
app.py CHANGED
@@ -1,10 +1,12 @@
1
  import os
2
  import json
3
  import firebase_admin
 
4
  from firebase_admin import credentials, firestore
5
  from flask import Flask, request, jsonify
6
  from huggingface_hub import InferenceClient
7
 
 
8
  app = Flask(__name__)
9
 
10
  # =============================
@@ -91,24 +93,40 @@ def analyze():
91
  return jsonify({"error": "Campo 'text' 茅 obrigat贸rio"}), 400
92
 
93
  text = data["text"]
94
-
95
  print(f"\n馃 Recebido texto para an谩lise: {text}\n")
96
 
97
- # Chamada segura via API gen茅rica
98
- response = client.post(
99
- json={"inputs": text},
100
- model="pysentimiento/robertuito-emotion-analysis",
101
- task="text-classification"
102
- )
 
 
 
 
 
 
 
 
 
 
 
 
103
 
104
- print(f"馃摡 Resposta bruta do modelo:\n{json.dumps(response, indent=2, ensure_ascii=False)}\n")
 
 
 
105
 
106
- if not isinstance(response, list) or len(response) == 0:
107
- raise ValueError("Resposta inesperada da API do Hugging Face")
108
 
109
- # Processa probabilidades
 
 
110
  probabilities = {}
111
- for item in response:
112
  label = item.get("label", "").lower()
113
  score = round(float(item.get("score", 0.0)), 4)
114
  emotion_pt = map_emotion(label, score)
 
1
  import os
2
  import json
3
  import firebase_admin
4
+ import requests
5
  from firebase_admin import credentials, firestore
6
  from flask import Flask, request, jsonify
7
  from huggingface_hub import InferenceClient
8
 
9
+
10
  app = Flask(__name__)
11
 
12
  # =============================
 
93
  return jsonify({"error": "Campo 'text' 茅 obrigat贸rio"}), 400
94
 
95
  text = data["text"]
 
96
  print(f"\n馃 Recebido texto para an谩lise: {text}\n")
97
 
98
+ # =============================
99
+ # Chamada direta 脿 API do Hugging Face
100
+ # =============================
101
+ HF_MODEL = "pysentimiento/robertuito-emotion-analysis"
102
+ HF_URL = f"https://api-inference.huggingface.co/models/{HF_MODEL}"
103
+
104
+ headers = {
105
+ "Authorization": f"Bearer {HF_TOKEN}",
106
+ "Content-Type": "application/json"
107
+ }
108
+
109
+ payload = {"inputs": text}
110
+
111
+ response = requests.post(HF_URL, headers=headers, json=payload)
112
+ response.raise_for_status()
113
+
114
+ result_json = response.json()
115
+ print(f"馃摡 Resposta bruta do modelo:\n{json.dumps(result_json, indent=2, ensure_ascii=False)}\n")
116
 
117
+ # Alguns modelos retornam [ [ {label, score}, ... ] ]
118
+ # Outros apenas [ {label, score}, ... ]
119
+ if isinstance(result_json, list) and isinstance(result_json[0], list):
120
+ result_json = result_json[0]
121
 
122
+ if not isinstance(result_json, list) or len(result_json) == 0:
123
+ raise ValueError("Resposta inesperada da API Hugging Face.")
124
 
125
+ # =============================
126
+ # Processamento das emo莽玫es
127
+ # =============================
128
  probabilities = {}
129
+ for item in result_json:
130
  label = item.get("label", "").lower()
131
  score = round(float(item.get("score", 0.0)), 4)
132
  emotion_pt = map_emotion(label, score)