File size: 2,142 Bytes
3dbf89f
 
2a055e3
3dbf89f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a218b95
 
 
 
3dbf89f
cd05c1e
a218b95
 
c19830e
 
 
 
a218b95
c19830e
 
915c689
e985eb9
a218b95
915c689
a218b95
c19830e
e985eb9
a218b95
 
 
c19830e
a218b95
3dbf89f
02b7135
3e8bc57
 
a218b95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b63d5df
a218b95
 
 
 
02b7135
a218b95
3dbf89f
b63d5df
 
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
import os
import time
import itertools
import gradio as gr
import google.generativeai as genai

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

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

chat_history = []

def generate_chat(prompt: str, chat_messages=chat_history):
    print(chat_messages)
    context = "You are an intelligent chatbot powered by biggest technology company."
    print("Generating Chat Message...")
    print(f"User Message:\n{prompt}\n")
    chat_messages.append(prompt)
    try:
        response = genai.chat(
          **chat_defaults,
          context=context,
          messages=chat_messages
        )
        result = response.last
        if result is None:
            result = "Apologies but something went wrong. Please try again later."
            chat_messages = chat_messages[:-1]
        else:
            chat_messages.append(result)
    except Exception as e:
        result = "Apologies but something went wrong. Please try again later."
        chat_messages = chat_messages[:-1]
        print(f"Exception {e} occured\n")
    chat_history = chat_messages
    print(f"Bot Message:\n{result}\n")
    return result

with gr.Blocks() as app:
    chatbot = gr.Chatbot(height=400, bubble_full_width=False, container=False)
    msg = gr.Textbox(label="Type your message...", value="Hi Gerard, can you introduce yourself?", container=False)
    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"""
    Hosted on 🤗 Spaces. Powered by Gradio & Google PaLM.
    """)
    
app.queue()
app.launch()