hoshingakag commited on
Commit
b63d5df
1 Parent(s): 7f82a6a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -63
app.py CHANGED
@@ -7,20 +7,6 @@ import google.generativeai as genai
7
  genai.configure(api_key=os.getenv('PALM_API_KEY'))
8
 
9
  # Gradio
10
- title = '🦒Playground w/ Google PaLM v2'
11
- description = """Click below tab and start with your message in English"""
12
-
13
- def generate_text(prompt: str):
14
- print("Generating Text...")
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 = "Apologies but something went wrong. Please try again later."
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',
@@ -32,69 +18,43 @@ chat_defaults = {
32
 
33
  chat_history = []
34
 
35
- def generate_chat(prompt: str, chat_messages=chat_history):
36
- print(chat_messages)
37
  context = "You are an intelligent chatbot powered by biggest technology company."
38
  print("Generating Chat Message...")
39
- print(f"User Message:\n{prompt}\n")
40
- chat_messages.append(prompt)
 
41
  try:
42
  response = genai.chat(
43
  **chat_defaults,
44
  context=context,
45
- messages=chat_messages
46
  )
47
  result = response.last
48
  if result is None:
49
  result = "Apologies but something went wrong. Please try again later."
50
- chat_messages = chat_messages[:-1]
51
  else:
52
- chat_messages.append(result)
53
  except Exception as e:
54
  result = "Apologies but something went wrong. Please try again later."
55
- chat_messages = chat_messages[:-1]
56
- print(f"Exception {e} occured\n")
57
- chat_history = chat_messages
 
58
  print(f"Bot Message:\n{result}\n")
59
- return result
60
-
61
- with gr.Blocks(theme='JohnSmith9982/small_and_pretty') as demo:
62
- gr.Markdown(
63
- f"""
64
- # {title}
65
- ### {description}
66
- """)
67
 
68
- with gr.Tab('Just Text'):
69
- app = gr.Interface(fn=generate_text, inputs=["text"], outputs=["text"],
70
- examples=[["What is Large Language Model?"], ["Write me a story about Pokemon."]]
71
- )
72
-
73
- with gr.Tab('Just Chat'):
74
- chatbot = gr.Chatbot(height=500)
75
- msg = gr.Textbox()
76
- clear = gr.Button("Clear")
77
-
78
- def user(user_message, history):
79
- return "", history + [[user_message, None]]
80
-
81
- def bot(history):
82
- bot_message = generate_chat(history[-1][0])
83
- history[-1][1] = ""
84
- for character in bot_message:
85
- history[-1][1] += character
86
- time.sleep(0.01)
87
- yield history
88
-
89
- msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
90
- bot, chatbot, chatbot
91
- )
92
- clear.click(lambda: None, None, chatbot, queue=False)
93
 
94
- gr.Markdown(
95
- f"""
96
- Testing by __G__
97
- """)
 
 
 
 
 
98
 
99
- demo.queue()
100
- demo.launch()
 
7
  genai.configure(api_key=os.getenv('PALM_API_KEY'))
8
 
9
  # Gradio
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  chat_defaults = {
12
  'model': 'models/chat-bison-001',
 
18
 
19
  chat_history = []
20
 
21
+ def generate_chat(message: str, history=chat_history):
22
+ print(history)
23
  context = "You are an intelligent chatbot powered by biggest technology company."
24
  print("Generating Chat Message...")
25
+ print(f"User Message:\n{message}\n")
26
+ history.append(message)
27
+
28
  try:
29
  response = genai.chat(
30
  **chat_defaults,
31
  context=context,
32
+ messages=history
33
  )
34
  result = response.last
35
  if result is None:
36
  result = "Apologies but something went wrong. Please try again later."
37
+ history = history[:-1]
38
  else:
39
+ history.append(result)
40
  except Exception as e:
41
  result = "Apologies but something went wrong. Please try again later."
42
+ history = history[:-1]
43
+ print(f"Exception {e}\n")
44
+
45
+ chat_history = history
46
  print(f"Bot Message:\n{result}\n")
 
 
 
 
 
 
 
 
47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
+ for i in range(result):
50
+ time.sleep(0.01)
51
+ yield result[: i+1]
52
+
53
+ app = gr.ChatInterface(
54
+ generate_chat,
55
+ additional_inputs=additional_inputs,
56
+ examples=[["What is the secret to life?"], ["Write me a recipe for pancakes."]]
57
+ )
58
 
59
+ app.queue()
60
+ app.launch()