Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,33 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
|
|
|
| 3 |
|
|
|
|
| 4 |
hf_token = os.getenv("HF_TOKEN")
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import os
|
| 3 |
+
import requests
|
| 4 |
|
| 5 |
+
# Retrieve the Hugging Face API token from the environment variable
|
| 6 |
hf_token = os.getenv("HF_TOKEN")
|
| 7 |
|
| 8 |
+
api_url = "https://api-inference.huggingface.co/models/SARAL-Influencer-OS/comment_analyser"
|
| 9 |
+
|
| 10 |
+
headers = {"Authorization": f"Bearer {hf_token}"} if hf_token else {}
|
| 11 |
+
|
| 12 |
+
label_map = {
|
| 13 |
+
"LABEL_0": "Buying Intent",
|
| 14 |
+
"LABEL_1": "Neutral Intent",
|
| 15 |
+
"LABEL_2": "Negative Intent"
|
| 16 |
+
}
|
| 17 |
+
|
| 18 |
+
def predict_comment(comment):
|
| 19 |
+
response = requests.post(api_url, headers=headers, json={"inputs": comment})
|
| 20 |
+
output = response.json()
|
| 21 |
|
| 22 |
+
# Map the model output to the corresponding label names
|
| 23 |
+
predictions = {label_map[item['label']]: item['score'] for item in output[0]}
|
| 24 |
+
return predictions
|
| 25 |
+
|
| 26 |
+
iface = gr.Interface(
|
| 27 |
+
fn=predict_comment,
|
| 28 |
+
inputs=gr.inputs.Textbox(lines=2, placeholder="Enter your comment..."),
|
| 29 |
+
outputs=gr.outputs.Label(num_top_classes=3)
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
# Launch the interface
|
| 33 |
+
iface.launch()
|