hoshingakag commited on
Commit
c19830e
1 Parent(s): 04f1469

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -11
app.py CHANGED
@@ -11,8 +11,16 @@ title = '🦒Playground w/ Google PaLM v2'
11
  description = """Click below tab and start with your message"""
12
 
13
  def generate_text(prompt: str):
14
- response = genai.generate_text(prompt=prompt)
15
- return response.result
 
 
 
 
 
 
 
 
16
 
17
  chat_defaults = {
18
  'model': 'models/chat-bison-001',
@@ -26,14 +34,24 @@ chat_messages = []
26
 
27
  def generate_chat(prompt: str):
28
  context = "You are an intelligent chatbot powered by biggest technology company."
 
 
29
  chat_messages.append(prompt)
30
- response = genai.chat(
31
- **chat_defaults,
32
- context=context,
33
- messages=chat_messages
34
- )
35
- chat_messages.append(response.last)
36
- return response.last
 
 
 
 
 
 
 
 
37
 
38
  with gr.Blocks(theme='JohnSmith9982/small_and_pretty') as demo:
39
 
@@ -56,9 +74,7 @@ with gr.Blocks(theme='JohnSmith9982/small_and_pretty') as demo:
56
  return "", history + [[user_message, None]]
57
 
58
  def bot(history):
59
- print(f"User Message: {history[-1][0]}\n")
60
  bot_message = generate_chat(history[-1][0])
61
- print(f"Bot Message: {bot_message}")
62
  history[-1][1] = ""
63
  for character in bot_message:
64
  history[-1][1] += character
 
11
  description = """Click below tab and start with your message"""
12
 
13
  def generate_text(prompt: str):
14
+ print("\n")
15
+ print(f"User Message:\n{prompt}\n")
16
+ try:
17
+ response = genai.generate_text(prompt=prompt)
18
+ result = response.result
19
+ except Exception as e:
20
+ result = "Something went wrong. Please try again with other prompts."
21
+ print(f"Exception {e} occured\n")
22
+ print(f"Bot Message:\n{result}\n")
23
+ return result
24
 
25
  chat_defaults = {
26
  'model': 'models/chat-bison-001',
 
34
 
35
  def generate_chat(prompt: str):
36
  context = "You are an intelligent chatbot powered by biggest technology company."
37
+ print("\n")
38
+ print(f"User Message:\n{prompt}\n")
39
  chat_messages.append(prompt)
40
+
41
+ try:
42
+ response = genai.chat(
43
+ **chat_defaults,
44
+ context=context,
45
+ messages=chat_messages
46
+ )
47
+ result = response.last
48
+ chat_messages.append(result)
49
+ except Exception as e:
50
+ result = "Something went wrong. Please try again with other messages."
51
+ chat_messages = chat_messages[:-1]
52
+ print(f"Exception {e} occured\n")
53
+ print(f"Bot Message:\n{result}\n")
54
+ return result
55
 
56
  with gr.Blocks(theme='JohnSmith9982/small_and_pretty') as demo:
57
 
 
74
  return "", history + [[user_message, None]]
75
 
76
  def bot(history):
 
77
  bot_message = generate_chat(history[-1][0])
 
78
  history[-1][1] = ""
79
  for character in bot_message:
80
  history[-1][1] += character