Spaces:
Running
Running
| 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) | |