Delete app.py
Browse files
app.py
DELETED
|
@@ -1,96 +0,0 @@
|
|
| 1 |
-
#Gardio to create user interface for chat
|
| 2 |
-
#Pytorch for model porcessing
|
| 3 |
-
#Pretrained language model and tokenizer
|
| 4 |
-
#spaces imported to manage GPU memory
|
| 5 |
-
import gradio as gr
|
| 6 |
-
import torch
|
| 7 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 8 |
-
|
| 9 |
-
device = torch.device('cuda' if torch.cuda.is_available() else "cpu")
|
| 10 |
-
|
| 11 |
-
#model used: from transformers library - example used is Qwen/Qwen2–0.5B-Instruct
|
| 12 |
-
#The tokenizer is loaded to convert the user’s input into a form that the model can understand (tokenization).
|
| 13 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 14 |
-
"Qwen/Qwen2-0.5B-Instruct",
|
| 15 |
-
torch_dtype=torch.float32, #changed from auto to float32 for my mac compatibility
|
| 16 |
-
device_map='auto' #can change from auto to none to avoid automatic offloading to cpu
|
| 17 |
-
) #.to(device)
|
| 18 |
-
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2-0.5B-Instruct")
|
| 19 |
-
|
| 20 |
-
#decorator used to signal that the subsequent function “def chatbot()” can use GPU memory
|
| 21 |
-
#@spaces.GPU
|
| 22 |
-
|
| 23 |
-
#assigning role to chatbot with system message, and message definition. Also checks for history (previous messages)
|
| 24 |
-
def chatbot(user_input, history):
|
| 25 |
-
system_message = {"role": "system", "content": "You are a helpful assistant."}
|
| 26 |
-
messages = history + [{"role": "user", "content": user_input}]
|
| 27 |
-
|
| 28 |
-
if len(history) == 0:
|
| 29 |
-
messages.insert(0, system_message)
|
| 30 |
-
|
| 31 |
-
text = tokenizer.apply_chat_template(
|
| 32 |
-
messages,
|
| 33 |
-
tokenize=False,
|
| 34 |
-
add_generation_prompt=True
|
| 35 |
-
|
| 36 |
-
#prepared text is converted into tokens the model understands (model_input) and resulting 'pt' is moved to device (CPU/GPU)
|
| 37 |
-
#the 'important' part of the input is signaled with attention_marsk but in this case it is all input - thats why useing torch.ones
|
| 38 |
-
model_inputs = tokenizer([text], return_tensors="pt").to(device)
|
| 39 |
-
attention_mask = torch.ones(model_inputs.input_ids.shape, device=device)
|
| 40 |
-
|
| 41 |
-
#input forms the base that the model will generate in generated_ids. Set limit for maximum tokens produced in accordance to memory space
|
| 42 |
-
#the generated text will be converted back into a readable text entered into response
|
| 43 |
-
#gradio app only understands certain format so to store this new response into chat history we transfer all to gradio_history
|
| 44 |
-
generated_ids = model.generate(
|
| 45 |
-
model_inputs.input_ids,
|
| 46 |
-
attention_mask=attention_mask,
|
| 47 |
-
max_new_tokens=512
|
| 48 |
-
)
|
| 49 |
-
|
| 50 |
-
generated_ids = [
|
| 51 |
-
output_ids[len(input_ids):] for input_ids, output_ids in zip(model_inputs.input_ids, generated_ids)
|
| 52 |
-
]
|
| 53 |
-
|
| 54 |
-
response = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)[0]
|
| 55 |
-
|
| 56 |
-
history.append({"role": "user", "content": user_input})
|
| 57 |
-
history.append({"role": "assistant", "content": response})
|
| 58 |
-
gradio_history = [[msg["role"], msg["content"]] for msg in history]
|
| 59 |
-
|
| 60 |
-
return gradio_history, history
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
#user interface of Gradio web app is created structured in blocks.
|
| 64 |
-
#containing typical chatbot setting, with history saved and textbox to make entries
|
| 65 |
-
#If the user makes an input “txt.sumit()”, the function “chatbot()” is called and the output is displayed in the chat window “chatbot_interface”
|
| 66 |
-
|
| 67 |
-
with gr.Blocks() as demo:
|
| 68 |
-
chatbot_interface = gr.Chatbot()
|
| 69 |
-
state = gr.State([])
|
| 70 |
-
|
| 71 |
-
with gr.Row():
|
| 72 |
-
txt = gr.Textbox(show_label=False, placeholder="Gib hier eine Nachricht ein...")
|
| 73 |
-
txt.submit(chatbot, [txt, state], [chatbot_interface, state])
|
| 74 |
-
|
| 75 |
-
demo.launch()
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|