3kmfi6hp/temp-sentiment-script / sentiment_analysis.py
3kmfi6hp's picture
download
raw
1.8 kB
import json, urllib.request, time
A = 'https://3kmfi6hp-finbert-sentiment-api.hf.space/predict'
B = 'https://3kmfi6hp-twitter-finbert-sentiment-api.hf.space/predict'
T = {
'uzbekistan': 'Will Uzbekistan win the 2026 FIFA World Cup? Uzbekistan is a huge underdog with Yes price at 0.05 cents. Virtually no chance.',
'new-zealand': 'Will New Zealand win the 2026 FIFA World Cup? New Zealand Yes price is 0.05 cents. Extremely unlikely to win.',
'qatar': 'Will Qatar win the 2026 FIFA World Cup? Qatar Yes price at 0.05 cents. Very long shot.',
'spain': 'Will Spain win the 2026 FIFA World Cup? Spain Yes at 13.9 cents, price has been rising steadily for 30 days. Strong contender.',
'argentina': 'Will Argentina win the 2026 FIFA World Cup? Argentina Yes at 15.1 cents, defending champion. One of the favorites.',
'france': 'Will France win the 2026 FIFA World Cup? France Yes at 19.4 cents, highest among all teams. Tournament favorite with Mbappe.'
}
def call_api(url, txt):
p = json.dumps({'text': txt}).encode()
q = urllib.request.Request(url, data=p, headers={'Content-Type': 'application/json'}, method='POST')
with urllib.request.urlopen(q, timeout=30) as r:
return json.loads(r.read().decode())
results = {}
for slug, text in T.items():
fb = call_api(A, text)
tw = call_api(B, text)
results[slug] = {
'title': text,
'finbert': {'sentiment': fb['sentiment'], 'confidence': fb['confidence']},
'twitter': {'sentiment': tw['sentiment'], 'confidence': tw['confidence']}
}
print(f'{slug}: FinBERT={fb["sentiment"]}({fb["confidence"]}) Twitter={tw["sentiment"]}({tw["confidence"]})', flush=True)
time.sleep(0.3)
print('===JSON_OUTPUT===')
print(json.dumps(results, indent=2))
print('===JSON_OUTPUT_END===')

Xet Storage Details

Size:
1.8 kB
·
Xet hash:
c90d87e59c0e695e44c9633644442e4d949046ffc6c9c20c1668b9158e503251

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.