File size: 1,932 Bytes
57d0588
e648b17
46bef1e
8274426
e648b17
b033a49
b40d855
 
e648b17
 
 
 
 
b40d855
e648b17
77f3cd9
e648b17
b40d855
e648b17
 
 
 
b40d855
e648b17
7ddaf2b
1b555fc
833b4dd
709456d
0c6b528
83db21c
89c9051
4c1fcca
080128f
4c1fcca
 
83db21c
7ddaf2b
4c1fcca
83db21c
 
 
4c1fcca
83db21c
 
 
 
 
 
 
 
 
 
 
 
a512dcd
83db21c
 
 
080128f
83db21c
080128f
722bf42
83db21c
722bf42
540249f
89c9051
4ac79c2
c062166
83db21c
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
import gradio as gr
import os
import time
import google.generativeai as palm

palm.configure(api_key=os.environ.get("palm_key"))

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

context = "Your IT assistant"

examples = [
    [
        "Hey my computer is broken",
        "Hey, what is the issue with your computer?"
    ]
]

history = ['']

with gr.Blocks(theme=gr.themes.Soft()) as demo:
    chatbot = gr.Chatbot()
    msg = gr.Textbox()
    btn = gr.Button("Submit", variant="primary")
    clear = gr.Button("Clear")
    
    def user(user_message, history):
        history.append([user_message, None])
        return gr.update(value=""), history
    
    def bot(history):
        try:
            bot_message = palm.chat(
                context=context,
                examples=examples,
                messages=[h[0] for h in history]
            )
            
            history[-1][1] = ""
            for character in bot_message.last:
                history[-1][1] += character
                time.sleep(0.005)
        except Exception as e:
            # Handle the exception here
            print("Error occurred:", str(e))
            # You can customize the error handling as per your requirements
            # For example, return an error message to the user
            
            history[-1][1] = "Incorrect input please retry with a longer sentence in english"
        
        return history

    response = msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
        bot, chatbot, chatbot
    )
    response = btn.click(user, [msg, chatbot], [msg, chatbot], queue=False).then(
        bot, chatbot, chatbot
    )
    response.then(lambda: gr.update(interactive=True), None, [msg], queue=False)
    clear.click(lambda: None, None, chatbot, queue=False)

demo.queue()
demo.launch(debug=True)