t4ai commited on
Commit
b580698
1 Parent(s): ec2568d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -0
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ import os
3
+ from dotenv import load_dotenv
4
+ import json
5
+ import gradio as gr
6
+ import random
7
+ import time
8
+ import requests
9
+
10
+ from transformers import BertModel, BertTokenizerFast, AdamW
11
+ import tensorflow as tf
12
+
13
+
14
+
15
+ load_dotenv(override=True)
16
+ if not os.getenv("HF_API_KEY"):
17
+ raise ValueError("HF_API_KEY must be set")
18
+ hf_key = os.getenv('HF_API_KEY')
19
+
20
+ API_URL = "https://api-inference.huggingface.co/models/t4ai/distilbert-finetuned-t3-qa"
21
+ headers = {"Authorization": "Bearer " + hf_key }
22
+
23
+ def query_model(payload):
24
+ response = requests.post(API_URL, headers=headers, json=payload)
25
+ return response.json()
26
+
27
+
28
+ # contruct UI using Gradio
29
+ _booted = False
30
+ with gr.Blocks() as demo:
31
+
32
+ with gr.Row():
33
+ with gr.Column(scale=1):
34
+ context = gr.Textbox(label="Document Text", lines=25)
35
+ with gr.Column(scale=2):
36
+ chatbot = gr.Chatbot(label="T3Soft Bot", value=[(None, "Welcome! I am your QA assistant."), (None, "Please paste your document content in the panel to the left."), (None, "Then submit questions below!")])
37
+ msg = gr.Textbox(label="Ask your question")
38
+ clear = gr.ClearButton([msg, chatbot])
39
+ _chatbot = chatbot
40
+
41
+ def respond(message, context, chat_history):
42
+
43
+ if(len(context) == 0):
44
+ bot_message = "Hm, I don't see any document text, please paste in the box on the left."
45
+ else:
46
+ query_bot = query_model({"inputs": {"question": message, "context": context}})
47
+ if(len(query_bot)):
48
+ bot_message = query_bot['answer']
49
+ else:
50
+ bot_message = "I'm having trouble with this question, please try again."
51
+
52
+
53
+ chat_history.append((message, bot_message))
54
+ time.sleep(2)
55
+ return "", context, chat_history
56
+
57
+ msg.submit(respond, [msg, context, chatbot], [msg, context, chatbot])
58
+
59
+ demo.launch()
60
+