Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import random
|
| 3 |
+
import time
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 5 |
+
|
| 6 |
+
# Initialize the model and tokenizer
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained("upstage/llama-30b-instruct-2048")
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained("upstage/llama-30b-instruct-2048")
|
| 9 |
+
|
| 10 |
+
# Define the bot function that will generate the responses
|
| 11 |
+
def bot(history):
|
| 12 |
+
user_message = history[-1][0]
|
| 13 |
+
input_ids = tokenizer.encode(user_message + tokenizer.eos_token, return_tensors='pt')
|
| 14 |
+
response = model.generate(input_ids, max_length=1000, pad_token_id=tokenizer.eos_token_id)
|
| 15 |
+
bot_message = tokenizer.decode(response[:, input_ids.shape[-1]:][0], skip_special_tokens=True)
|
| 16 |
+
history[-1][1] = ""
|
| 17 |
+
for character in bot_message:
|
| 18 |
+
history[-1][1] += character
|
| 19 |
+
time.sleep(0.05)
|
| 20 |
+
yield history
|
| 21 |
+
|
| 22 |
+
# Define the functions to add text and files to the chat history
|
| 23 |
+
def add_text(history, text):
|
| 24 |
+
history = history + [(text, None)]
|
| 25 |
+
return history, gr.update(value="", interactive=False)
|
| 26 |
+
|
| 27 |
+
def add_file(history, file):
|
| 28 |
+
history = history + [((file.name,), None)]
|
| 29 |
+
return history
|
| 30 |
+
|
| 31 |
+
# Initialize the Gradio interface
|
| 32 |
+
with gr.Blocks() as demo:
|
| 33 |
+
chatbot = gr.Chatbot([], elem_id="chatbot").style(height=750)
|
| 34 |
+
with gr.Row():
|
| 35 |
+
with gr.Column(scale=0.85):
|
| 36 |
+
txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter, or upload an image").style(container=False)
|
| 37 |
+
with gr.Column(scale=0.15, min_width=0):
|
| 38 |
+
btn = gr.UploadButton("📁", file_types=["image", "video", "audio"])
|
| 39 |
+
txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(bot, chatbot, chatbot)
|
| 40 |
+
txt_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False)
|
| 41 |
+
file_msg = btn.upload(add_file, [chatbot, btn], [chatbot], queue=False).then(bot, chatbot, chatbot)
|
| 42 |
+
|
| 43 |
+
# Run the chatbot
|
| 44 |
+
demo.queue()
|
| 45 |
+
demo.launch()
|