Spaces:
Runtime error
Runtime error
Update app.py
Browse files6 unmodified lines78910111211 unmodified lines242526272829305 unmodified lines3637383940414213 unmodified lines565758596061624 unmodified lines676869707172737475767778798081828363 unmodified lines14714814915015115215345 unmodified lines1992002012022032042056 unmodified linesfrom transformers import Pix2StructForConditionalGeneration, Pix2StructProcessorfrom peft import PeftModel## CoT promptsdef _add_markup(table):11 unmodified lines# just use the raw table if parsing failsreturn table_TABLE = """Year | Democrats | Republicans | Independents2004 | 68.1% | 45.0% | 53.0%2006 | 58.0% | 42.0% | 53.0%5 unmodified lines_INSTRUCTION = 'Read the table below to answer the following questions.'_TEMPLATE = f"""First read an example then the complete question for the second table.------------{_INSTRUCTION}13 unmodified lines## alpaca-lora# debugging...assert ("LlamaTokenizer" in transformers._import_structure["models.llama"]), "LLaMA is now in HuggingFace's main branch.\nPlease reinstall it: pip uninstall transformers && pip install git+https://github.com/huggingface/transformers.git"4 unmodified linesBASE_MODEL = "decapoda-research/llama-7b-hf"LORA_WEIGHTS = "tloen/alpaca-lora-7b"if torch.cuda.is_available():device = "cuda"else:device = "cpu"try:if torch.backends.mps.is_available():device = "mps"except:passif device == "cuda":model = LlamaForCausalLM.from_pretrained(BASE_MODEL,63 unmodified linesreturn ret## deplot modelsmodel_deplot = Pix2StructForConditionalGeneration.from_pretrained("google/deplot", torch_dtype=torch.bfloat16).to(0)processor_deplot = Pix2StructProcessor.from_pretrained("google/deplot")def evaluate(45 unmodified linesdef process_document(image, question, llm):# image = Image.open(image)inputs = processor_deplot(images=image, text="Generate the underlying data table for the figure below:", return_tensors="pt").to(0, torch.bfloat16)predictions = model_deplot.generate(**inputs, max_new_tokens=512)table = processor_deplot.decode(predictions[0], skip_special_tokens=True).replace("<0x0A>", "\n")6 unmodified lines78910111213141516171819202122232411 unmodified lines3637383940415 unmodified lines47484950515213 unmodified lines6667686970714 unmodified lines76777879808163 unmodified lines14514614714814915015115215315445 unmodified lines2002012022032042052062072086 unmodified linesfrom transformers import Pix2StructForConditionalGeneration, Pix2StructProcessorfrom peft import PeftModelif torch.cuda.is_available():device = "cuda"else:device = "cpu"try:if torch.backends.mps.is_available():device = "mps"except:pass## CoT promptsdef _add_markup(table):11 unmodified lines# just use the raw table if parsing failsreturn table_TABLE = """Year | Democrats | Republicans | Independents2004 | 68.1% | 45.0% | 53.0%2006 | 58.0% | 42.0% | 53.0%5 unmodified lines_INSTRUCTION = 'Read the table below to answer the following questions.'_TEMPLATE = f"""First read an example then the complete question for the second table.------------{_INSTRUCTION}13 unmodified lines## alpaca-loraassert ("LlamaTokenizer" in transformers._import_structure["models.llama"]), "LLaMA is now in HuggingFace's main branch.\nPlease reinstall it: pip uninstall transformers && pip install git+https://github.com/huggingface/transformers.git"4 unmodified linesBASE_MODEL = "decapoda-research/llama-7b-hf"LORA_WEIGHTS = "tloen/alpaca-lora-7b"if device == "cuda":model = LlamaForCausalLM.from_pretrained(BASE_MODEL,63 unmodified linesreturn ret## deplot modelsif device == "cuda":model_deplot = Pix2StructForConditionalGeneration.from_pretrained("google/deplot", torch_dtype=torch.bfloat16).to(0)else:model_deplot = Pix2StructForConditionalGeneration.from_pretrained("google/deplot")processor_deplot = Pix2StructProcessor.from_pretrained("google/deplot")def evaluate(45 unmodified linesdef process_document(image, question, llm):# image = Image.open(image)inputs = processor_deplot(images=image, text="Generate the underlying data table for the figure below:", return_tensors="pt")if device == "cuda":inputs = inputs.to(0, torch.bfloat16)predictions = model_deplot.generate(**inputs, max_new_tokens=512)table = processor_deplot.decode(predictions[0], skip_special_tokens=True).replace("<0x0A>", "\n")
app.py+19-16