File size: 1,408 Bytes
e9a7fa6
 
 
4f4d4b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
48
49
50
51
52
53
54
55
56
import os
os.system('pip install gradio==2.3.5b0')

import gradio as gr

from transformers import pipeline

import wikipedia


talk = 0
su = ''
nlp_qa = pipeline('question-answering')

def chat(message):
    history = gr.get_state() or []


    global talk
    global su

    if talk == 0:
      response = 'tell me a subject you wanted to talk about'
      talk = 1
    
    elif talk == 1:
      results = wikipedia.search(message)
      summary = wikipedia.summary(results)
      su = summary
      response = 'ask me a question about this topic'
      talk = 2
    
    else:
      a = nlp_qa(context=su, question=message)
      response = list(a.values())[3]
      talk = 0



    history.append((message, response))
    gr.set_state(history)
    html = "<div class='chatbot'>"
    for user_msg, resp_msg in history:
        html += f"<div class='user_msg'>{user_msg}</div>"
        html += f"<div class='resp_msg'>{resp_msg}</div>"
    html += "</div>"
    return html

iface = gr.Interface(chat, "text", "html", css="""
    .chatbox {display:flex;flex-direction:column}
    .user_msg, .resp_msg {padding:4px;margin-bottom:4px;border-radius:4px;width:80%}
    .user_msg {background-color:cornflowerblue;color:white;align-self:start}
    .resp_msg {background-color:lightgray;align-self:self-end}
""", allow_screenshot=False, allow_flagging=False)
if __name__ == "__main__":
    iface.launch(debug=True)