File size: 1,182 Bytes
9846a25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7c60370
05d51e3
9846a25
 
 
 
 
 
 
 
 
 
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
'''
Author: i316984 daisy.mao@sap.com
Date: 2024-02-16 12:02:26
LastEditors: i316984 daisy.mao@sap.com
LastEditTime: 2024-02-16 12:30:20
FilePath: /undefined/Users/i316984_1/hackaithon101/app.py
Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
'''

import gradio as gr
import requests
import os
import json

API_URL = "https://api-inference.huggingface.co/models/cardiffnlp/twitter-roberta-base-sentiment"

bt = os.environ['HACKAITHONBEARERTOKEN']
headers = {"Authorization": bt }
key_mapping = {'LABEL_0':'negative','LABEL_1':'neutral','LABEL_2':'positive'}

def query(data):
    response = requests.post(API_URL, headers=headers, json=data)
    list_json = response.json()
    json_resp = list_json[0]
    return "V2"+json.dumps([{k: key_mapping.get(v, v) for k, v in temp_dict.items()} for temp_dict in json_resp], indent=4)


def greet(howareyoufeeling):
    output = query({"inputs":howareyoufeeling})
    print (str(output))
    return str(output)

iface = gr.Interface(
  fn=greet, inputs=["text"], outputs="text",  allow_flagging="never")
iface.launch()