hoshingakag commited on
Commit
3dbf89f
1 Parent(s): 24d3076

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -0
app.py ADDED
@@ -0,0 +1,77 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import time
3
+ import gradio as gr
4
+ import google.generativeai as genai
5
+
6
+ # Credentials
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"""
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',
19
+ 'temperature': 0.25,
20
+ 'candidate_count': 1,
21
+ 'top_k': 40,
22
+ 'top_p': 0,
23
+ }
24
+
25
+ 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
+
40
+ gr.Markdown(
41
+ f"""
42
+ # {title}
43
+ ## {description}
44
+ """)
45
+
46
+ with gr.Tab('Just Text'):
47
+ app = gr.Interface(fn=generate_text, inputs=["text"], outputs=["text"],
48
+ examples=[["What is Large Language Model?"], ["Write me a story about Pokemon."]])
49
+
50
+ with gr.Tab('Just Chat'):
51
+ chatbot = gr.Chatbot(height=600)
52
+ msg = gr.Textbox()
53
+ clear = gr.Button("Clear")
54
+
55
+ def user(user_message, history):
56
+ return "", history + [[user_message, None]]
57
+
58
+ def bot(history):
59
+ bot_message = generate_chat(history[-1][0])
60
+ history[-1][1] = ""
61
+ for character in bot_message:
62
+ history[-1][1] += character
63
+ time.sleep(0.01)
64
+ yield history
65
+
66
+ msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
67
+ bot, chatbot, chatbot
68
+ )
69
+ clear.click(lambda: None, None, chatbot, queue=False)
70
+
71
+ gr.Markdown(
72
+ f"""
73
+ Testing by __G__
74
+ """)
75
+
76
+ demo.queue()
77
+ demo.launch()