Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import requests
|
| 5 |
+
|
| 6 |
+
API_TOKEN = os.environ['API_TOKEN']
|
| 7 |
+
|
| 8 |
+
state = []
|
| 9 |
+
|
| 10 |
+
headers = {'Authorization': f'Bearer {API_TOKEN}'}
|
| 11 |
+
|
| 12 |
+
def query(payload, model_id):
|
| 13 |
+
API_URL = f'https://api-inference.huggingface.co/models/{model_id}'
|
| 14 |
+
response = requests.post(API_URL, headers=headers, json=payload)
|
| 15 |
+
return response.json()
|
| 16 |
+
|
| 17 |
+
def chat(message):
|
| 18 |
+
response = query({
|
| 19 |
+
'inputs': message,
|
| 20 |
+
'parameters': {
|
| 21 |
+
'max_length': 500,
|
| 22 |
+
}
|
| 23 |
+
}, 'IssakaAI/health-chatbot')
|
| 24 |
+
reply = ''
|
| 25 |
+
if isinstance(response, list):
|
| 26 |
+
reply = response[0]['generated_text'][len(message) + 1:]
|
| 27 |
+
elif isinstance(response, dict):
|
| 28 |
+
reply = f'{response["error"]}. Please wait about {int(response["estimated_time"])} seconds.'
|
| 29 |
+
state.append((message, reply))
|
| 30 |
+
return gr.Textbox.update(value=''), state
|
| 31 |
+
|
| 32 |
+
def clear_message():
|
| 33 |
+
state.clear()
|
| 34 |
+
return gr.Chatbot.update(value=[])
|
| 35 |
+
|
| 36 |
+
with gr.Blocks() as blk:
|
| 37 |
+
gr.Markdown('# Interact with IssakaAI NLP models')
|
| 38 |
+
with gr.Row():
|
| 39 |
+
chatbot = gr.Chatbot()
|
| 40 |
+
with gr.Box():
|
| 41 |
+
message = gr.Textbox(value='What is the menstrual cycle?', lines=10)
|
| 42 |
+
send = gr.Button('Send', variant='primary')
|
| 43 |
+
clear = gr.Button('Clear history', variant='secondary')
|
| 44 |
+
send.click(fn=chat, inputs=message, outputs=[message, chatbot])
|
| 45 |
+
clear.click(fn=clear_message, inputs=[], outputs=chatbot)
|
| 46 |
+
blk.launch(debug=True)
|