Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,85 +1,92 @@
|
|
| 1 |
-
import
|
|
|
|
|
|
|
| 2 |
import torch
|
| 3 |
-
from
|
| 4 |
-
|
| 5 |
-
from
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
# Load and process the document
|
| 14 |
-
doc_loader = TextLoader("dataset.txt")
|
| 15 |
-
docs = doc_loader.load()
|
| 16 |
-
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
|
| 17 |
-
split_docs = text_splitter.split_documents(docs)
|
| 18 |
-
|
| 19 |
-
# Create embeddings and vector store
|
| 20 |
-
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
|
| 21 |
-
vectordb = FAISS.from_documents(split_docs, embeddings)
|
| 22 |
-
|
| 23 |
-
# Load model and tokenizer
|
| 24 |
-
model_name = "01-ai/Yi-Coder-9B-Chat"
|
| 25 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 26 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 27 |
-
model_name,
|
| 28 |
-
device_map="auto",
|
| 29 |
-
torch_dtype=torch.float16 if device == "cuda" else torch.float32
|
| 30 |
-
)
|
| 31 |
|
| 32 |
-
# Set up the QA pipeline
|
| 33 |
-
qa_pipeline = pipeline(
|
| 34 |
-
"text-generation",
|
| 35 |
-
model=model,
|
| 36 |
-
tokenizer=tokenizer,
|
| 37 |
-
max_new_tokens=750,
|
| 38 |
-
pad_token_id=tokenizer.eos_token_id
|
| 39 |
-
)
|
| 40 |
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
|
| 53 |
-
if "
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
def clean_response(response):
|
| 58 |
-
result = response.get("result", "")
|
| 59 |
-
if "Answer:" in result:
|
| 60 |
-
return result.split("Answer:")[1].strip()
|
| 61 |
-
return result.strip()
|
| 62 |
|
| 63 |
-
|
| 64 |
-
processed_query = preprocess_query(user_input)
|
| 65 |
-
raw_response = qa_chain.invoke({"query": processed_query})
|
| 66 |
-
return clean_response(raw_response)
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
send_button = gr.Button("Send")
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
send_button.click(interact, inputs=[user_input, chat_history], outputs=[chat_history, chat_history])
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import MllamaForConditionalGeneration, AutoProcessor, TextIteratorStreamer
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import requests
|
| 4 |
import torch
|
| 5 |
+
from threading import Thread
|
| 6 |
+
import gradio as gr
|
| 7 |
+
from gradio import FileData
|
| 8 |
+
import time
|
| 9 |
+
import spaces
|
| 10 |
+
ckpt = "mrcuddle/llama3.2-11B-Vision_instruct-Coder"
|
| 11 |
+
model = MllamaForConditionalGeneration.from_pretrained(ckpt,
|
| 12 |
+
torch_dtype=torch.bfloat16).to("cuda")
|
| 13 |
+
processor = AutoProcessor.from_pretrained(ckpt)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
@spaces.GPU
|
| 17 |
+
def bot_streaming(message, history, max_new_tokens=250):
|
| 18 |
+
|
| 19 |
+
txt = message["text"]
|
| 20 |
+
ext_buffer = f"{txt}"
|
| 21 |
+
|
| 22 |
+
messages= []
|
| 23 |
+
images = []
|
| 24 |
+
|
| 25 |
|
| 26 |
+
for i, msg in enumerate(history):
|
| 27 |
+
if isinstance(msg[0], tuple):
|
| 28 |
+
messages.append({"role": "user", "content": [{"type": "text", "text": history[i+1][0]}, {"type": "image"}]})
|
| 29 |
+
messages.append({"role": "assistant", "content": [{"type": "text", "text": history[i+1][1]}]})
|
| 30 |
+
images.append(Image.open(msg[0][0]).convert("RGB"))
|
| 31 |
+
elif isinstance(history[i-1], tuple) and isinstance(msg[0], str):
|
| 32 |
+
# messages are already handled
|
| 33 |
+
pass
|
| 34 |
+
elif isinstance(history[i-1][0], str) and isinstance(msg[0], str): # text only turn
|
| 35 |
+
messages.append({"role": "user", "content": [{"type": "text", "text": msg[0]}]})
|
| 36 |
+
messages.append({"role": "assistant", "content": [{"type": "text", "text": msg[1]}]})
|
| 37 |
|
| 38 |
+
# add current message
|
| 39 |
+
if len(message["files"]) == 1:
|
| 40 |
+
|
| 41 |
+
if isinstance(message["files"][0], str): # examples
|
| 42 |
+
image = Image.open(message["files"][0]).convert("RGB")
|
| 43 |
+
else: # regular input
|
| 44 |
+
image = Image.open(message["files"][0]["path"]).convert("RGB")
|
| 45 |
+
images.append(image)
|
| 46 |
+
messages.append({"role": "user", "content": [{"type": "text", "text": txt}, {"type": "image"}]})
|
| 47 |
+
else:
|
| 48 |
+
messages.append({"role": "user", "content": [{"type": "text", "text": txt}]})
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
texts = processor.apply_chat_template(messages, add_generation_prompt=True)
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
if images == []:
|
| 54 |
+
inputs = processor(text=texts, return_tensors="pt").to("cuda")
|
| 55 |
+
else:
|
| 56 |
+
inputs = processor(text=texts, images=images, return_tensors="pt").to("cuda")
|
| 57 |
+
streamer = TextIteratorStreamer(processor, skip_special_tokens=True, skip_prompt=True)
|
|
|
|
| 58 |
|
| 59 |
+
generation_kwargs = dict(inputs, streamer=streamer, max_new_tokens=max_new_tokens)
|
| 60 |
+
generated_text = ""
|
| 61 |
+
|
| 62 |
+
thread = Thread(target=model.generate, kwargs=generation_kwargs)
|
| 63 |
+
thread.start()
|
| 64 |
+
buffer = ""
|
| 65 |
+
|
| 66 |
+
for new_text in streamer:
|
| 67 |
+
buffer += new_text
|
| 68 |
+
generated_text_without_prompt = buffer
|
| 69 |
+
time.sleep(0.01)
|
| 70 |
+
yield buffer
|
| 71 |
|
|
|
|
| 72 |
|
| 73 |
+
demo = gr.ChatInterface(fn=bot_streaming, title="Multimodal Llama",examples=[
|
| 74 |
+
[{"text": "Replicate this webpage using Tyepescript and ChakraUI.", "files":["./examples/Untitled.png"]},
|
| 75 |
+
2000],
|
| 76 |
+
],
|
| 77 |
+
textbox=gr.MultimodalTextbox(),
|
| 78 |
+
additional_inputs = [gr.Slider(
|
| 79 |
+
minimum=10,
|
| 80 |
+
maximum=2500,
|
| 81 |
+
value=500,
|
| 82 |
+
step=10,
|
| 83 |
+
label="Maximum number of new tokens to generate",
|
| 84 |
+
)
|
| 85 |
+
],
|
| 86 |
+
cache_examples=False,
|
| 87 |
+
description="Yes, this space can replicate (to the model's best ability) a webpage in your preferred language.",
|
| 88 |
+
stop_btn="Stop Generation",
|
| 89 |
+
fill_height=True,
|
| 90 |
+
multimodal=True)
|
| 91 |
+
|
| 92 |
+
demo.launch(debug=True)
|