Antoine245 commited on
Commit
83db21c
·
1 Parent(s): 850fa76

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -24
app.py CHANGED
@@ -3,7 +3,7 @@ import os
3
  import time
4
  import google.generativeai as palm
5
 
6
- palm.configure(api_key=os.environ.get("palm_key"))
7
 
8
  defaults = {
9
  'model': 'models/chat-bison-001',
@@ -22,41 +22,46 @@ examples = [
22
  ]
23
  ]
24
 
25
- # user_message = ['']
26
  history = ['']
27
 
28
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
29
  chatbot = gr.Chatbot()
30
  msg = gr.Textbox()
31
- btn = gr.Button("Submit",variant="primary")
32
- # gr.ClearButton([msg, chatbot])
33
- clear = gr.Button("Clear")
34
 
35
-
36
  def user(user_message, history):
37
- return gr.update(value=""), history + [[user_message,None]]
38
- # , interactive=False
39
  def bot(history):
40
- # Remove the second element of the last element of the list
41
- # del history[-1][1]
42
- bot_message = palm.chat(
43
- context=context,
44
- examples=examples,
45
- messages=history[-1][0]
46
- )
47
- history[-1][1] = ""
48
- for character in bot_message.last:
49
- history[-1][1] += character
50
- time.sleep(0.005)
51
- yield history
 
 
 
 
 
 
 
 
 
52
  response = msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
53
- bot, chatbot, chatbot
54
  )
55
  response = btn.click(user, [msg, chatbot], [msg, chatbot], queue=False).then(
56
- bot, chatbot, chatbot
57
  )
58
  response.then(lambda: gr.update(interactive=True), None, [msg], queue=False)
59
- clear.click(lambda: None, None, chatbot, queue=False)
60
 
61
  demo.queue()
62
- demo.launch()
 
3
  import time
4
  import google.generativeai as palm
5
 
6
+ palm.configure(api_key=api_key)
7
 
8
  defaults = {
9
  'model': 'models/chat-bison-001',
 
22
  ]
23
  ]
24
 
 
25
  history = ['']
26
 
27
  with gr.Blocks(theme=gr.themes.Soft()) as demo:
28
  chatbot = gr.Chatbot()
29
  msg = gr.Textbox()
30
+ btn = gr.Button("Submit", variant="primary")
31
+ gr.ClearButton([msg, chatbot])
 
32
 
 
33
  def user(user_message, history):
34
+ return gr.update(value=""), history + [[user_message, None]]
35
+
36
  def bot(history):
37
+ try:
38
+ bot_message = palm.chat(
39
+ context=context,
40
+ examples=examples,
41
+ messages=history[-1][0]
42
+ )
43
+
44
+ history[-1][1] = ""
45
+ for character in bot_message.last:
46
+ history[-1][1] += character
47
+ time.sleep(0.005)
48
+ except Exception as e:
49
+ # Handle the exception here
50
+ print("Error occurred:", str(e))
51
+ # You can customize the error handling as per your requirements
52
+ # For example, return an error message to the user
53
+
54
+ history[-1][1] = "Incorrect input please retry"
55
+
56
+ return history
57
+
58
  response = msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
59
+ bot, chatbot, chatbot
60
  )
61
  response = btn.click(user, [msg, chatbot], [msg, chatbot], queue=False).then(
62
+ bot, chatbot, chatbot
63
  )
64
  response.then(lambda: gr.update(interactive=True), None, [msg], queue=False)
 
65
 
66
  demo.queue()
67
+ demo.launch(debug=True)