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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import json
|
| 3 |
+
import os
|
| 4 |
+
import boto3
|
| 5 |
+
|
| 6 |
+
dump_controls = False
|
| 7 |
+
log_to_console = False
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def add_text(history, text):
|
| 11 |
+
history = history + [(text, None)]
|
| 12 |
+
return history, gr.Textbox(value="", interactive=False)
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def add_file(history, file):
|
| 16 |
+
with open(file.name, mode="rb") as f:
|
| 17 |
+
content = f.read()
|
| 18 |
+
|
| 19 |
+
if isinstance(content, bytes):
|
| 20 |
+
content = content.decode('utf-8', 'replace')
|
| 21 |
+
else:
|
| 22 |
+
content = str(content)
|
| 23 |
+
|
| 24 |
+
fn = os.path.basename(file.name)
|
| 25 |
+
history = history + [(f'```{fn}\n{content}\n```', None)]
|
| 26 |
+
|
| 27 |
+
gr.Info(f"File added as {fn}")
|
| 28 |
+
|
| 29 |
+
return history
|
| 30 |
+
|
| 31 |
+
def submit_text(txt_value):
|
| 32 |
+
return add_text([chatbot, txt_value], [chatbot, txt_value])
|
| 33 |
+
|
| 34 |
+
def undo(history):
|
| 35 |
+
history.pop()
|
| 36 |
+
return history
|
| 37 |
+
|
| 38 |
+
def dump(history):
|
| 39 |
+
return str(history)
|
| 40 |
+
|
| 41 |
+
def load_settings():
|
| 42 |
+
# Dummy Python function, actual loading is done in JS
|
| 43 |
+
pass
|
| 44 |
+
|
| 45 |
+
def save_settings(acc, sec, prompt, temp):
|
| 46 |
+
# Dummy Python function, actual saving is done in JS
|
| 47 |
+
pass
|
| 48 |
+
|
| 49 |
+
def process_values_js():
|
| 50 |
+
return """
|
| 51 |
+
() => {
|
| 52 |
+
return ["access_key", "secret_key", "token"];
|
| 53 |
+
}
|
| 54 |
+
"""
|
| 55 |
+
|
| 56 |
+
def bot(message, history, aws_access, aws_secret, aws_token):
|
| 57 |
+
try:
|
| 58 |
+
prompt = "\n\n"
|
| 59 |
+
for human, assi in history:
|
| 60 |
+
if prompt is not None:
|
| 61 |
+
prompt += f"Human: {human}\n\n"
|
| 62 |
+
if assi is not None:
|
| 63 |
+
prompt += f"Assistant: {assi}\n\n"
|
| 64 |
+
if message:
|
| 65 |
+
prompt += f"Human: {message}\n\n"
|
| 66 |
+
prompt += f"Assistant:"
|
| 67 |
+
|
| 68 |
+
if log_to_console:
|
| 69 |
+
print(f"br_prompt: {str(prompt)}")
|
| 70 |
+
|
| 71 |
+
body = json.dumps({
|
| 72 |
+
"prompt": prompt,
|
| 73 |
+
"max_tokens_to_sample": 300,
|
| 74 |
+
"temperature": 0,
|
| 75 |
+
})
|
| 76 |
+
|
| 77 |
+
sess = boto3.Session(
|
| 78 |
+
aws_access_key_id=aws_access,
|
| 79 |
+
aws_secret_access_key=aws_secret,
|
| 80 |
+
aws_session_token=aws_token,
|
| 81 |
+
region_name='eu-central-1')
|
| 82 |
+
br = sess.client(service_name="bedrock-runtime")
|
| 83 |
+
|
| 84 |
+
response = br.invoke_model(body=body, modelId="anthropic.claude-v2",
|
| 85 |
+
accept="application/json", contentType="application/json")
|
| 86 |
+
response_body = json.loads(response.get('body').read())
|
| 87 |
+
br_result = response_body.get('completion')
|
| 88 |
+
|
| 89 |
+
history[-1][1] = br_result
|
| 90 |
+
if log_to_console:
|
| 91 |
+
print(f"br_result: {str(history)}")
|
| 92 |
+
|
| 93 |
+
except Exception as e:
|
| 94 |
+
raise gr.Error(f"Error: {str(e)}")
|
| 95 |
+
|
| 96 |
+
return "", history
|
| 97 |
+
|
| 98 |
+
with gr.Blocks() as demo:
|
| 99 |
+
gr.Markdown("# Amazon™️ Bedrock™️ Chat™️ (Nils' Version™️) feat. Anthropic™️ Claude-2™️")
|
| 100 |
+
|
| 101 |
+
with gr.Accordion("Settings"):
|
| 102 |
+
aws_access = gr.Textbox(label="AWS Access Key", elem_id="aws_access")
|
| 103 |
+
aws_secret = gr.Textbox(label="AWS Secret Key", elem_id="aws_secret")
|
| 104 |
+
aws_token = gr.Textbox(label="AWS Session Token", elem_id="aws_token")
|
| 105 |
+
system_prompt = gr.TextArea("You are a helpful AI.", label="System Prompt", lines=3, max_lines=250, elem_id="system_prompt")
|
| 106 |
+
temp = gr.Slider(0, 1, label="Temperature", elem_id="temp")
|
| 107 |
+
save_button = gr.Button("Save Settings")
|
| 108 |
+
load_button = gr.Button("Load Settings")
|
| 109 |
+
|
| 110 |
+
load_button.click(load_settings, js="""
|
| 111 |
+
() => {
|
| 112 |
+
let elems = ['#aws_access textarea', '#aws_secret textarea', '#aws_token textarea', '#system_prompt textarea', '#temp input'];
|
| 113 |
+
elems.forEach(elem => {
|
| 114 |
+
let item = document.querySelector(elem);
|
| 115 |
+
let event = new InputEvent('input', { bubbles: true });
|
| 116 |
+
item.value = localStorage.getItem(elem.split(" ")[0].slice(1)) || '';
|
| 117 |
+
item.dispatchEvent(event);
|
| 118 |
+
});
|
| 119 |
+
}
|
| 120 |
+
""")
|
| 121 |
+
|
| 122 |
+
save_button.click(save_settings, [aws_access, aws_secret, aws_token, system_prompt, temp], js="""
|
| 123 |
+
(acc, sec, tok, prompt, temp) => {
|
| 124 |
+
localStorage.setItem('aws_access', acc);
|
| 125 |
+
localStorage.setItem('aws_secret', sec);
|
| 126 |
+
localStorage.setItem('aws_token', tok);
|
| 127 |
+
localStorage.setItem('system_prompt', prompt);
|
| 128 |
+
localStorage.setItem('temp', temp);
|
| 129 |
+
}
|
| 130 |
+
""")
|
| 131 |
+
|
| 132 |
+
chatbot = gr.Chatbot(
|
| 133 |
+
[],
|
| 134 |
+
elem_id="chatbot",
|
| 135 |
+
show_copy_button=True,
|
| 136 |
+
height=350
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
with gr.Row():
|
| 140 |
+
txt = gr.TextArea(
|
| 141 |
+
scale=4,
|
| 142 |
+
show_label=False,
|
| 143 |
+
placeholder="Enter text and press enter, or upload a file",
|
| 144 |
+
container=False,
|
| 145 |
+
lines=3,
|
| 146 |
+
)
|
| 147 |
+
submit_btn = gr.Button("🚀 Send", scale=0)
|
| 148 |
+
submit_click = submit_btn.click(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
|
| 149 |
+
bot, [txt, chatbot, aws_access, aws_secret, aws_token], [txt, chatbot],
|
| 150 |
+
)
|
| 151 |
+
submit_click.then(lambda: gr.Textbox(interactive=True), None, [txt], queue=False)
|
| 152 |
+
|
| 153 |
+
with gr.Row():
|
| 154 |
+
btn = gr.UploadButton("📁 Upload", size="sm")
|
| 155 |
+
undo_btn = gr.Button("↩️ Undo")
|
| 156 |
+
undo_btn.click(undo, inputs=[chatbot], outputs=[chatbot])
|
| 157 |
+
|
| 158 |
+
clear = gr.ClearButton(chatbot, value="🗑️ Clear")
|
| 159 |
+
|
| 160 |
+
if dump_controls:
|
| 161 |
+
with gr.Row():
|
| 162 |
+
dmp_btn = gr.Button("Dump")
|
| 163 |
+
txt_dmp = gr.Textbox("Dump")
|
| 164 |
+
dmp_btn.click(dump, inputs=[chatbot], outputs=[txt_dmp])
|
| 165 |
+
|
| 166 |
+
txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(
|
| 167 |
+
bot, [txt, chatbot, aws_access, aws_secret, aws_token], [txt, chatbot],
|
| 168 |
+
)
|
| 169 |
+
txt_msg.then(lambda: gr.Textbox(interactive=True), None, [txt], queue=False)
|
| 170 |
+
file_msg = btn.upload(add_file, [chatbot, btn], [chatbot], queue=False, postprocess=False)
|
| 171 |
+
|
| 172 |
+
demo.queue().launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
langchain
|
| 3 |
+
boto3
|