File size: 2,061 Bytes
dba8da4 ed3ffd7 801a229 dba8da4 80b81e1 dba8da4 80b81e1 dba8da4 4136d58 80b81e1 b173edb 80b81e1 4136d58 93fed63 de98e3d 93fed63 80b81e1 4136d58 80b81e1 4136d58 ec705bd 80b81e1 ec705bd dba8da4 ec705bd |
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 |
import requests
import urllib.parse
import gradio as gr
API_URL = "https://api-inference.huggingface.co/models/ProsusAI/finbert"
headers = {"Authorization": "Bearer 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"}
# def query(payload):
# response = requests.post(API_URL, headers=headers, json=payload)
# sentiment_prediction = response.json()
# pos, neg, neutr = sentiment_prediction[:, 0].item(), sentiment_prediction[:, 1].item(), sentiment_prediction[:, 2].item()
# sentiment_label = "Positive" if pos > neg and pos > neutr else "Negative" if neg > pos and neg > neutr else "Neutral"
# return sentiment_label
# output = query({
# "inputs": "I like you. I love you",
# })
def predict_sentiment(payload):
# Sentiment Analysis
response = requests.post(API_URL, headers=headers, json=payload)
sentiment_prediction = response.json()
# with torch.no_grad():
# sentiment_outputs = requests.post(API_URL, headers=headers, json=payload)
# sentiment_prediction = torch.nn.functional.softmax(sentiment_outputs.logits, dim=-1)
for sentiment_prediction in response_json:
if sentiment_prediction['label'] == 'positive':
pos = sentiment_prediction['score']
elif sentiment_prediction['label'] == 'neutral':
neutr = sentiment_prediction['score']
elif sentiment_prediction['label'] == 'negative':
neg = sentiment_prediction['score']
# pos, neg, neutr = sentiment_prediction[:, 0].item(), sentiment_prediction[:, 1].item(), sentiment_prediction[:, 2].item()
sentiment_label = "Positive" if pos > neg and pos > neutr else "Negative" if neg > pos and neg > neutr else "Neutral"
return sentiment_label
# Gradio Interface
iface = gr.Interface(
fn=predict_sentiment,
inputs=[gr.Textbox(lines=2, label="Financial Statement")],
outputs=[
gr.Textbox(label="Sentiment"),
],
live=True,
title="Financial Content Sentiment Analysis",
description="Enter a financial statement to analyze its sentiment."
)
iface.launch() |