Team Ai
Apppublic

IssakaAI/web-interface

sourceHugging Faceupdated 4y agoView on Hugging Face
0likes
app.py85 linesDownload Raw Back to root
1import os2 3import gradio as gr4import requests5 6API_TOKEN = os.environ['API_TOKEN']7G_TRANS_API_TOKEN = os.environ['G_TRANS_API_TOKEN']8 9API_URL = 'https://api-inference.huggingface.co/models/{}'10G_TRANS_API = 'https://translation.googleapis.com/language/translate/v2'11headers = {'Authorization': f'Bearer {API_TOKEN}'}12 13def detect_lang(message):14    response = requests.get(G_TRANS_API+'/detect', params={'key': G_TRANS_API_TOKEN, 'q': message})15    return response.json()16 17def translate_src_to_en(message, src_lang):18    response = requests.get(G_TRANS_API, params={'key': G_TRANS_API_TOKEN, 'source': src_lang, 'target': 'en', 'q': message})19    return response.json()20 21def translate_en_to_src(message, src_lang):22    response = requests.get(G_TRANS_API, params={'key': G_TRANS_API_TOKEN, 'source': 'en', 'target': src_lang, 'q': message})23    return response.json()24 25def query_model(model_id, payload):26    response = requests.post(API_URL.format(model_id), headers=headers, json=payload)27    return response.json()28 29def parse_model_response(response):30    return response[0]['generated_text']31 32def parse_model_error(response):33    return f'{response["error"]}. Please wait about {int(response["estimated_time"])} seconds.'34 35def parse_translation_response(response):36    return response['data']['translations'][0]['translatedText']37 38def query_model(model_id, payload):39    response = requests.post(API_URL.format(model_id), headers=headers, json=payload)40    return response.json()41 42state = []43 44def chat(message, multi):45    message_en = message46    if multi:47        response = detect_lang(message)48        lang = response['data']['detections'][0][0]['language'][:2]49        if lang != 'en':50            response = translate_src_to_en(message, lang)51            message_en = parse_translation_response(response)52    response = query_model('IssakaAI/health-chatbot', {53        'inputs': message_en,54        'parameters': {55            'max_length': 500,56        }57    })58    reply = ''59    if isinstance(response, list):60        reply = parse_model_response(response)[len(message_en) + 1:]61        if multi and lang != 'en':62            response = translate_en_to_src(reply, lang)63            reply = parse_translation_response(response)64    elif isinstance(response, dict):65        reply = parse_model_error(response)66    state.append((message, reply))67    return gr.Textbox.update(value=''), state68 69def clear_message():70    state.clear()71    return gr.Chatbot.update(value=[])72 73with gr.Blocks() as blk:74    gr.Markdown('# Interact with IssakaAI NLP models')75    with gr.Row():76        chatbot = gr.Chatbot()77        with gr.Box():78            message = gr.Textbox(value='What is the menstrual cycle?', lines=10)79            multi = gr.Checkbox(False, label='Multilingual chatbot')80            send = gr.Button('Send', variant='primary')81            clear = gr.Button('Clear history', variant='secondary')82    send.click(fn=chat, inputs=[message, multi], outputs=[message, chatbot])83    clear.click(fn=clear_message, inputs=[], outputs=chatbot)84blk.launch(debug=True)85