Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -25,8 +25,12 @@ def respond(message, history):
|
|
25 |
if cfg.streaming:
|
26 |
# Call stream response and stream output
|
27 |
stream = vq.submit_query_streaming(message)
|
|
|
|
|
|
|
28 |
for output in stream:
|
29 |
-
|
|
|
30 |
else:
|
31 |
# Call non-stream response and return message output
|
32 |
response = vq.submit_query(message)
|
@@ -39,26 +43,34 @@ def vote(data: gr.LikeData):
|
|
39 |
print("Received Thumbs down")
|
40 |
|
41 |
heading_html = f'''
|
42 |
-
<table>
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
<td colspan="2" style="font-size: 16px;">This demo uses Retrieval Augmented Generation from <a href="https://vectara.com/">Vectara</a> to ask questions about {cfg.source_data_desc}.</td>
|
53 |
-
</tr>
|
54 |
-
</table>
|
55 |
-
'''
|
56 |
|
57 |
bot_css = """
|
58 |
-
table {
|
59 |
-
|
60 |
-
|
61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
"""
|
63 |
|
64 |
if cfg.examples:
|
@@ -66,48 +78,126 @@ if cfg.examples:
|
|
66 |
else:
|
67 |
app_examples = None
|
68 |
|
69 |
-
with gr.Blocks(
|
70 |
-
gr.
|
71 |
-
chatbot = gr.Chatbot(value=[[None, "How may I help you?"]])
|
72 |
-
msg = gr.Textbox(label="Message")
|
73 |
-
clear = gr.Button("Clear")
|
74 |
-
|
75 |
-
def user(message, history):
|
76 |
-
return "", history + [[message, None]]
|
77 |
-
|
78 |
-
def bot(history):
|
79 |
-
message = history[-1][0]
|
80 |
-
bot_message = respond(message, history)
|
81 |
-
|
82 |
-
if cfg.streaming:
|
83 |
-
full_response = ""
|
84 |
-
for chunk in bot_message:
|
85 |
-
full_response += chunk
|
86 |
-
history[-1][1] = full_response
|
87 |
-
yield history
|
88 |
-
else:
|
89 |
-
history[-1][1] = next(bot_message)
|
90 |
-
yield history
|
91 |
-
|
92 |
-
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
93 |
-
bot, chatbot, chatbot, api_name="bot_response"
|
94 |
-
)
|
95 |
chatbot.like(vote, None, None)
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
if app_examples:
|
100 |
-
gr.Examples(
|
101 |
-
app_examples,
|
102 |
-
inputs=msg,
|
103 |
-
outputs=chatbot,
|
104 |
-
fn=user,
|
105 |
-
cache_examples=False
|
106 |
-
)
|
107 |
|
108 |
if __name__ == "__main__":
|
109 |
demo.launch()
|
110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
|
112 |
|
113 |
# from omegaconf import OmegaConf
|
|
|
25 |
if cfg.streaming:
|
26 |
# Call stream response and stream output
|
27 |
stream = vq.submit_query_streaming(message)
|
28 |
+
|
29 |
+
|
30 |
+
outputs = ""
|
31 |
for output in stream:
|
32 |
+
outputs += output
|
33 |
+
yield outputs
|
34 |
else:
|
35 |
# Call non-stream response and return message output
|
36 |
response = vq.submit_query(message)
|
|
|
43 |
print("Received Thumbs down")
|
44 |
|
45 |
heading_html = f'''
|
46 |
+
<table>
|
47 |
+
<tr>
|
48 |
+
<td style="width: 80%; text-align: left; vertical-align: middle;"> <h1>Vectara AI Assistant: {cfg.title}</h1> </td>
|
49 |
+
<td style="width: 20%; text-align: right; vertical-align: middle;"> <img src="https://github.com/david-oplatka/chatbot-streamlit/blob/main/Vectara-logo.png?raw=true"> </td>
|
50 |
+
</tr>
|
51 |
+
<tr>
|
52 |
+
<td colspan="2" style="font-size: 16px;">This demo uses Retrieval Augmented Generation from <a href="https://vectara.com/">Vectara</a> to ask questions about {cfg.source_data_desc}.</td>
|
53 |
+
</tr>
|
54 |
+
</table>
|
55 |
+
'''
|
|
|
|
|
|
|
|
|
56 |
|
57 |
bot_css = """
|
58 |
+
table {
|
59 |
+
border: none;
|
60 |
+
width: 100%;
|
61 |
+
table-layout: fixed;
|
62 |
+
border-collapse: separate;
|
63 |
+
}
|
64 |
+
td {
|
65 |
+
vertical-align: middle;
|
66 |
+
border: none;
|
67 |
+
}
|
68 |
+
img {
|
69 |
+
width: 75%;
|
70 |
+
}
|
71 |
+
h1 {
|
72 |
+
font-size: 2em; /* Adjust the size as needed */
|
73 |
+
}
|
74 |
"""
|
75 |
|
76 |
if cfg.examples:
|
|
|
78 |
else:
|
79 |
app_examples = None
|
80 |
|
81 |
+
with gr.Blocks() as demo:
|
82 |
+
chatbot = gr.Chatbot(value = [[None, "How may I help you?"]])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
chatbot.like(vote, None, None)
|
84 |
+
gr.ChatInterface(respond, description = heading_html, css = bot_css,
|
85 |
+
chatbot = chatbot, examples = app_examples, cache_examples = False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
|
87 |
if __name__ == "__main__":
|
88 |
demo.launch()
|
89 |
|
90 |
+
|
91 |
+
# from omegaconf import OmegaConf
|
92 |
+
# from query import VectaraQuery
|
93 |
+
# import os
|
94 |
+
# import gradio as gr
|
95 |
+
|
96 |
+
# def isTrue(x) -> bool:
|
97 |
+
# if isinstance(x, bool):
|
98 |
+
# return x
|
99 |
+
# return x.strip().lower() == 'true'
|
100 |
+
|
101 |
+
# corpus_keys = str(os.environ['corpus_keys']).split(',')
|
102 |
+
# cfg = OmegaConf.create({
|
103 |
+
# 'corpus_keys': corpus_keys,
|
104 |
+
# 'api_key': str(os.environ['api_key']),
|
105 |
+
# 'title': os.environ['title'],
|
106 |
+
# 'source_data_desc': os.environ['source_data_desc'],
|
107 |
+
# 'streaming': isTrue(os.environ.get('streaming', False)),
|
108 |
+
# 'prompt_name': os.environ.get('prompt_name', None),
|
109 |
+
# 'examples': os.environ.get('examples', None)
|
110 |
+
# })
|
111 |
+
|
112 |
+
# vq = VectaraQuery(cfg.api_key, cfg.corpus_keys, cfg.prompt_name)
|
113 |
+
|
114 |
+
# def respond(message, history):
|
115 |
+
# if cfg.streaming:
|
116 |
+
# # Call stream response and stream output
|
117 |
+
# stream = vq.submit_query_streaming(message)
|
118 |
+
# for output in stream:
|
119 |
+
# yield output
|
120 |
+
# else:
|
121 |
+
# # Call non-stream response and return message output
|
122 |
+
# response = vq.submit_query(message)
|
123 |
+
# yield response
|
124 |
+
|
125 |
+
# def vote(data: gr.LikeData):
|
126 |
+
# if data.liked:
|
127 |
+
# print("Received Thumbs up")
|
128 |
+
# else:
|
129 |
+
# print("Received Thumbs down")
|
130 |
+
|
131 |
+
# heading_html = f'''
|
132 |
+
# <table>
|
133 |
+
# <tr>
|
134 |
+
# <td style="width: 80%; text-align: left; vertical-align: middle;">
|
135 |
+
# <h1>Vectara AI Assistant: {cfg.title}</h1>
|
136 |
+
# </td>
|
137 |
+
# <td style="width: 20%; text-align: right; vertical-align: middle;">
|
138 |
+
# <img src="https://github.com/david-oplatka/chatbot-streamlit/blob/main/Vectara-logo.png?raw=true">
|
139 |
+
# </td>
|
140 |
+
# </tr>
|
141 |
+
# <tr>
|
142 |
+
# <td colspan="2" style="font-size: 16px;">This demo uses Retrieval Augmented Generation from <a href="https://vectara.com/">Vectara</a> to ask questions about {cfg.source_data_desc}.</td>
|
143 |
+
# </tr>
|
144 |
+
# </table>
|
145 |
+
# '''
|
146 |
+
|
147 |
+
# bot_css = """
|
148 |
+
# table { border: none; width: 100%; table-layout: fixed; border-collapse: separate;}
|
149 |
+
# td { vertical-align: middle; border: none;}
|
150 |
+
# img { width: 75%;}
|
151 |
+
# h1 { font-size: 2em; /* Adjust the size as needed */}
|
152 |
+
# """
|
153 |
+
|
154 |
+
# if cfg.examples:
|
155 |
+
# app_examples = [example.strip() for example in cfg.examples.split(",")]
|
156 |
+
# else:
|
157 |
+
# app_examples = None
|
158 |
+
|
159 |
+
# with gr.Blocks(css=bot_css) as demo:
|
160 |
+
# gr.HTML(heading_html)
|
161 |
+
# chatbot = gr.Chatbot(value=[[None, "How may I help you?"]])
|
162 |
+
# msg = gr.Textbox(label="Message")
|
163 |
+
# clear = gr.Button("Clear")
|
164 |
+
|
165 |
+
# def user(message, history):
|
166 |
+
# return "", history + [[message, None]]
|
167 |
+
|
168 |
+
# def bot(history):
|
169 |
+
# message = history[-1][0]
|
170 |
+
# bot_message = respond(message, history)
|
171 |
+
|
172 |
+
# if cfg.streaming:
|
173 |
+
# full_response = ""
|
174 |
+
# for chunk in bot_message:
|
175 |
+
# full_response += chunk
|
176 |
+
# history[-1][1] = full_response
|
177 |
+
# yield history
|
178 |
+
# else:
|
179 |
+
# history[-1][1] = next(bot_message)
|
180 |
+
# yield history
|
181 |
+
|
182 |
+
# msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
183 |
+
# bot, chatbot, chatbot, api_name="bot_response"
|
184 |
+
# )
|
185 |
+
# chatbot.like(vote, None, None)
|
186 |
+
|
187 |
+
# clear.click(lambda: None, None, chatbot, queue=False)
|
188 |
+
|
189 |
+
# if app_examples:
|
190 |
+
# gr.Examples(
|
191 |
+
# app_examples,
|
192 |
+
# inputs=msg,
|
193 |
+
# outputs=chatbot,
|
194 |
+
# fn=user,
|
195 |
+
# cache_examples=False
|
196 |
+
# )
|
197 |
+
|
198 |
+
# if __name__ == "__main__":
|
199 |
+
# demo.launch()
|
200 |
+
|
201 |
|
202 |
|
203 |
# from omegaconf import OmegaConf
|