Spaces:
Sleeping
Sleeping
Create app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657import streamlit as stimport subprocessfrom PIL import Imageimport requestsfrom io import BytesIOfrom transformers import AutoModel, AutoTokenizerimport torch
torch.cuda.empty_cache()
# Load the model and tokenizer on CPUmodel = AutoModel.from_pretrained('openbmb/MiniCPM-Llama3-V-2_5-int4', trust_remote_code=True).to('cpu')tokenizer = AutoTokenizer.from_pretrained('openbmb/MiniCPM-Llama3-V-2_5-int4', trust_remote_code=True)model.eval()
# Title of the appst.title("Streamlit App with Image URL and Prompts")
# Text area for image URLimage_url = st.text_area("Enter Image URL:")
# Text area for system prompt inputsystem_prompt = st.text_area("Enter System Prompt:")
# Text area for user prompt inputquestion = st.text_area("Enter User Prompt:")
# Button to submit and display the imageif st.button("Submit"): if image_url: try: subprocess.run(['wget', image_url, "-O", 'flowchart.png']) response = requests.get(image_url) img = Image.open(BytesIO(response.content)) st.image(img, caption="Image from URL") except Exception as e: st.error(f"Error loading image. Please submit another image URL with a .png or .jpg extension: {e}") else: st.warning("Please enter an image URL.")
# Model code if system_prompt and question: image = Image.open('flowchart.png').convert('RGB') msgs = [{'role': 'user', 'content': question}]
res = model.chat( image=image, msgs=msgs, tokenizer=tokenizer, sampling=True, # if sampling=False, beam_search will be used by default temperature=0.7, system_prompt=system_prompt # pass system_prompt if needed )
st.text_area("Output:", value=res, height=200) else: st.warning("Please enter both system prompt and user prompt.")app.pyAdded+57