david-oplatka commited on
Commit
ea256e3
·
verified ·
1 Parent(s): 5df36a8

Change to Blocks Layout

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