Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
|
| 5 |
+
import wikipedia
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
talk = 0
|
| 9 |
+
su = ''
|
| 10 |
+
nlp_qa = pipeline('question-answering')
|
| 11 |
+
|
| 12 |
+
def chat(message):
|
| 13 |
+
history = gr.get_state() or []
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
global talk
|
| 17 |
+
global su
|
| 18 |
+
|
| 19 |
+
if talk == 0:
|
| 20 |
+
response = 'tell me a subject you wanted to talk about'
|
| 21 |
+
talk = 1
|
| 22 |
+
|
| 23 |
+
elif talk == 1:
|
| 24 |
+
results = wikipedia.search(message)
|
| 25 |
+
summary = wikipedia.summary(results)
|
| 26 |
+
su = summary
|
| 27 |
+
response = 'ask me a question about this topic'
|
| 28 |
+
talk = 2
|
| 29 |
+
|
| 30 |
+
else:
|
| 31 |
+
a = nlp_qa(context=su, question=message)
|
| 32 |
+
response = list(a.values())[3]
|
| 33 |
+
talk = 0
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
history.append((message, response))
|
| 38 |
+
gr.set_state(history)
|
| 39 |
+
html = "<div class='chatbot'>"
|
| 40 |
+
for user_msg, resp_msg in history:
|
| 41 |
+
html += f"<div class='user_msg'>{user_msg}</div>"
|
| 42 |
+
html += f"<div class='resp_msg'>{resp_msg}</div>"
|
| 43 |
+
html += "</div>"
|
| 44 |
+
return html
|
| 45 |
+
|
| 46 |
+
iface = gr.Interface(chat, "text", "html", css="""
|
| 47 |
+
.chatbox {display:flex;flex-direction:column}
|
| 48 |
+
.user_msg, .resp_msg {padding:4px;margin-bottom:4px;border-radius:4px;width:80%}
|
| 49 |
+
.user_msg {background-color:cornflowerblue;color:white;align-self:start}
|
| 50 |
+
.resp_msg {background-color:lightgray;align-self:self-end}
|
| 51 |
+
""", allow_screenshot=False, allow_flagging=False)
|
| 52 |
+
if __name__ == "__main__":
|
| 53 |
+
iface.launch(debug=True)
|