Spaces:
Sleeping
Sleeping
File size: 1,441 Bytes
d7ba0dd |
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 |
import os
import gradio as gr
import requests
API_TOKEN = os.environ['API_TOKEN']
state = []
headers = {'Authorization': f'Bearer {API_TOKEN}'}
def query(payload, model_id):
API_URL = f'https://api-inference.huggingface.co/models/{model_id}'
response = requests.post(API_URL, headers=headers, json=payload)
return response.json()
def chat(message):
response = query({
'inputs': message,
'parameters': {
'max_length': 500,
}
}, 'IssakaAI/health-chatbot')
reply = ''
if isinstance(response, list):
reply = response[0]['generated_text'][len(message) + 1:]
elif isinstance(response, dict):
reply = f'{response["error"]}. Please wait about {int(response["estimated_time"])} seconds.'
state.append((message, reply))
return gr.Textbox.update(value=''), state
def clear_message():
state.clear()
return gr.Chatbot.update(value=[])
with gr.Blocks() as blk:
gr.Markdown('# Interact with IssakaAI NLP models')
with gr.Row():
chatbot = gr.Chatbot()
with gr.Box():
message = gr.Textbox(value='What is the menstrual cycle?', lines=10)
send = gr.Button('Send', variant='primary')
clear = gr.Button('Clear history', variant='secondary')
send.click(fn=chat, inputs=message, outputs=[message, chatbot])
clear.click(fn=clear_message, inputs=[], outputs=chatbot)
blk.launch(debug=True)
|