File size: 2,764 Bytes
58c22f4
2f7ac45
 
3b91035
58c22f4
 
3b91035
58c22f4
 
de2bef1
2f7ac45
58c22f4
f0bf860
 
2d1be73
 
b220b73
 
 
 
 
 
 
 
 
 
 
 
 
 
3b91035
b220b73
 
 
 
 
 
f6b67bc
2f7ac45
58c22f4
 
 
 
 
2f7ac45
 
f0bf860
 
ac94949
 
 
 
 
b91ba2f
f0bf860
 
 
 
 
 
 
 
 
 
ac94949
 
 
f0bf860
 
231ebf8
5461adb
ac94949
e384144
ac94949
 
e384144
b220b73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58c22f4
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import os
import time
import gradio as gr
import google.generativeai as genai

# Credentials
genai.configure(api_key=os.getenv('PALM_API_KEY'))

# Gradio
title = '🦒Playground w/ Google PaLM v2'
description = """Click below tab and start with your message"""

def generate_text(prompt: str):
    response = genai.generate_text(prompt=prompt)
    return response.result

chat_defaults = {
  'model': 'models/chat-bison-001',
  'temperature': 0.25,
  'candidate_count': 1,
  'top_k': 40,
  'top_p': 0,
}

chat_messages = []

def generate_chat(prompt: str):
    context = "You are an intelligent chatbot powered by biggest technology company."
    chat_messages.append(prompt)
    response = genai.chat(
      **chat_defaults,
      context=context,
      messages=chat_messages
    )
    chat_messages.append(response.last)
    return response.last

with gr.Blocks(theme='JohnSmith9982/small_and_pretty') as demo:
    
    gr.Markdown(
    f"""
    # {title}
    ## {description}
    """)

    with gr.Tab('Just Text'):
        chatbot = gr.Chatbot(height=600)
        msg = gr.Textbox()
        clear = gr.Button("Clear")
        accordion = gr.Accordion("Generation History")

        def record(history):
            return history
        
        def user(user_message, history):
            return "", history + [[user_message, None]]
    
        def bot(history):
            bot_message = generate_text(history[-1][0])
            history[-1][1] = ""
            for character in bot_message:
                history[-1][1] += character
                time.sleep(0.01)
                yield history

        msg.submit(record, accordion, accordion)
        
        msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
            bot, chatbot, chatbot
        )

        clear.click(lambda: None, None, chatbot, queue=False)
        
            # for generation in store:
            #     gr.Markdown(f"Query: {generation[0]}\nAnswer: {generation[1]}")

    with gr.Tab('Just Chat'):
        chatbot = gr.Chatbot(height=600)
        msg = gr.Textbox()
        clear = gr.Button("Clear")
    
        def user(user_message, history):
            return "", history + [[user_message, None]]
    
        def bot(history):
            bot_message = generate_chat(history[-1][0])
            history[-1][1] = ""
            for character in bot_message:
                history[-1][1] += character
                time.sleep(0.01)
                yield history
    
        msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
            bot, chatbot, chatbot
        )
        clear.click(lambda: None, None, chatbot, queue=False)
    
    gr.Markdown(
    f"""
    Testing by __G__
    """)
    
demo.queue()
demo.launch()