Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +91 -38
src/streamlit_app.py
CHANGED
|
@@ -1,40 +1,93 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
import pandas as pd
|
| 4 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
""
|
| 7 |
-
# Welcome to Streamlit!
|
| 8 |
-
|
| 9 |
-
Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
|
| 10 |
-
If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
|
| 11 |
-
forums](https://discuss.streamlit.io).
|
| 12 |
-
|
| 13 |
-
In the meantime, below is an example of what you can do with just a few lines of code:
|
| 14 |
-
"""
|
| 15 |
-
|
| 16 |
-
num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
|
| 17 |
-
num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
|
| 18 |
-
|
| 19 |
-
indices = np.linspace(0, 1, num_points)
|
| 20 |
-
theta = 2 * np.pi * num_turns * indices
|
| 21 |
-
radius = indices
|
| 22 |
-
|
| 23 |
-
x = radius * np.cos(theta)
|
| 24 |
-
y = radius * np.sin(theta)
|
| 25 |
-
|
| 26 |
-
df = pd.DataFrame({
|
| 27 |
-
"x": x,
|
| 28 |
-
"y": y,
|
| 29 |
-
"idx": indices,
|
| 30 |
-
"rand": np.random.randn(num_points),
|
| 31 |
-
})
|
| 32 |
-
|
| 33 |
-
st.altair_chart(alt.Chart(df, height=700, width=700)
|
| 34 |
-
.mark_point(filled=True)
|
| 35 |
-
.encode(
|
| 36 |
-
x=alt.X("x", axis=None),
|
| 37 |
-
y=alt.Y("y", axis=None),
|
| 38 |
-
color=alt.Color("idx", legend=None, scale=alt.Scale()),
|
| 39 |
-
size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
|
| 40 |
-
))
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
|
|
|
|
| 3 |
import streamlit as st
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
+
|
| 7 |
+
import torch
|
| 8 |
+
from transformers import AutoModel, AutoTokenizer
|
| 9 |
+
|
| 10 |
+
@st.cache_resource(show_spinner=True)
|
| 11 |
+
def load_model():
|
| 12 |
+
model = AutoModel.from_pretrained("openbmb/MiniCPM-V", trust_remote_code=True, torch_dtype=torch.bfloat16)
|
| 13 |
+
tokenizer = AutoTokenizer.from_pretrained("openbmb/MiniCPM-V", trust_remote_code=True)
|
| 14 |
+
# send to GPU/CPU as per availability
|
| 15 |
+
if torch.cuda.is_available():
|
| 16 |
+
model = model.to(device='cuda', dtype=torch.bfloat16)
|
| 17 |
+
else:
|
| 18 |
+
model = model.to(device='cpu')
|
| 19 |
+
model.eval()
|
| 20 |
+
return model, tokenizer
|
| 21 |
+
|
| 22 |
+
model, tokenizer = load_model()
|
| 23 |
+
|
| 24 |
+
st.set_page_config(page_title="MiniCPM-V Chat", layout="wide")
|
| 25 |
+
st.title("📄 MiniCPM-V Chat — Image/Text → Markdown / Chat")
|
| 26 |
+
|
| 27 |
+
if "history" not in st.session_state:
|
| 28 |
+
st.session_state.history = []
|
| 29 |
+
|
| 30 |
+
# Sidebar: upload or text input
|
| 31 |
+
with st.sidebar:
|
| 32 |
+
uploaded_file = st.file_uploader("Upload image / pdf-page (image) or enter text:", type=["jpg","jpeg","png","pdf","txt"])
|
| 33 |
+
text_input = st.text_area("Or paste text here:")
|
| 34 |
+
|
| 35 |
+
# Main chat interface
|
| 36 |
+
for msg in st.session_state.history:
|
| 37 |
+
with st.chat_message(msg["role"]):
|
| 38 |
+
st.markdown(msg["content"])
|
| 39 |
+
|
| 40 |
+
def run_minicpm_v(input_image=None, input_text=None, history=None):
|
| 41 |
+
"""
|
| 42 |
+
input_image: PIL.Image or None
|
| 43 |
+
input_text: str or None
|
| 44 |
+
history: list of prior messages (role, content)
|
| 45 |
+
"""
|
| 46 |
+
msgs = []
|
| 47 |
+
if history:
|
| 48 |
+
msgs = history.copy()
|
| 49 |
+
# Compose new user message
|
| 50 |
+
user_content = ""
|
| 51 |
+
if input_image is not None:
|
| 52 |
+
user_content = "[Image Uploaded]\n" # or some marker + optional prompt
|
| 53 |
+
if input_text:
|
| 54 |
+
user_content += input_text
|
| 55 |
+
msgs.append({"role": "user", "content": user_content})
|
| 56 |
+
|
| 57 |
+
# Run the multimodal chat
|
| 58 |
+
res, context, _ = model.chat(
|
| 59 |
+
image=input_image,
|
| 60 |
+
msgs=msgs,
|
| 61 |
+
context=None,
|
| 62 |
+
tokenizer=tokenizer,
|
| 63 |
+
sampling=True,
|
| 64 |
+
temperature=0.7,
|
| 65 |
+
)
|
| 66 |
+
return res
|
| 67 |
+
|
| 68 |
+
if uploaded_file is not None or text_input:
|
| 69 |
+
with st.chat_message("user"):
|
| 70 |
+
if uploaded_file is not None:
|
| 71 |
+
st.image(uploaded_file, caption="Uploaded")
|
| 72 |
+
if text_input:
|
| 73 |
+
st.markdown(text_input)
|
| 74 |
+
# Process input
|
| 75 |
+
input_image = None
|
| 76 |
+
input_text = None
|
| 77 |
+
if uploaded_file is not None:
|
| 78 |
+
# try open as image
|
| 79 |
+
try:
|
| 80 |
+
input_image = Image.open(uploaded_file).convert("RGB")
|
| 81 |
+
except Exception as e:
|
| 82 |
+
st.error("Could not open uploaded file as image.")
|
| 83 |
+
if text_input:
|
| 84 |
+
input_text = text_input
|
| 85 |
+
|
| 86 |
+
with st.spinner("Thinking..."):
|
| 87 |
+
reply = run_minicpm_v(input_image=input_image, input_text=input_text, history=st.session_state.history)
|
| 88 |
+
|
| 89 |
+
st.session_state.history.append({"role": "assistant", "content": reply})
|
| 90 |
+
with st.chat_message("assistant"):
|
| 91 |
+
st.markdown(reply)
|
| 92 |
|
| 93 |
+
st.chat_input(placeholder="Send more text or upload another file…") # optional extra prompt
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|