Spaces:
Runtime error
Runtime error
Update src/streamlit_app.py
Browse files12345678910111213141516171819202122232425262728293031323334353637383940import altair as altimport numpy as npimport pandas as pdimport streamlit as st"""# Welcome to Streamlit!Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [communityforums](https://discuss.streamlit.io).In the meantime, below is an example of what you can do with just a few lines of code:"""num_points = st.slider("Number of points in spiral", 1, 10000, 1100)num_turns = st.slider("Number of turns in spiral", 1, 300, 31)indices = np.linspace(0, 1, num_points)theta = 2 * np.pi * num_turns * indicesradius = indicesx = radius * np.cos(theta)y = radius * np.sin(theta)df = pd.DataFrame({"x": x,"y": y,"idx": indices,"rand": np.random.randn(num_points),})st.altair_chart(alt.Chart(df, height=700, width=700).mark_point(filled=True).encode(x=alt.X("x", axis=None),y=alt.Y("y", axis=None),color=alt.Color("idx", legend=None, scale=alt.Scale()),size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),))1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889import streamlit as stfrom ultralytics import YOLOfrom PIL import Image, ImageDrawimport requestsfrom io import BytesIOimport os# Set tampilan dan judul aplikasist.set_page_config(layout="centered", page_title="CerfixScan", page_icon="π·")# Threshold deteksi confidencethreshold = 0.92# Fungsi untuk load model dari Hugging Face@st.cache_resourcedef load_model():model_path = "best.pt"if not os.path.exists(model_path):hf_url = "https://huggingface.co/rakharaditya/cerfixscan/resolve/main/best.pt"response = requests.get(hf_url)with open(model_path, "wb") as f:f.write(response.content)return YOLO(model_path)# Load model sekali sajamodel = load_model()# Judul halamanst.title("π· CerfixScan: Serviks Detection")st.markdown("Ambil gambar serviks via **kamera** atau **upload file**.")# Ambil input gambar dari kamera atau upload fileimg_input = st.camera_input("Ambil gambar dari kamera", key="camera")uploaded_file = st.file_uploader("Atau upload gambar dari galeri", type=["jpg", "jpeg", "png"])image = Noneif img_input is not None:image = Image.open(img_input).convert("RGB")elif uploaded_file is not None:image = Image.open(uploaded_file).convert("RGB")# Jika ada gambar, tampilkan dan lakukan deteksiif image:st.image(image, caption="Input Gambar", use_column_width=True)# Simpan gambar dalam memori (BytesIO), hindari file system jika mungkinbuffered = BytesIO()image.save(buffered, format="JPEG")buffered.seek(0)# Jalankan deteksiresults = model.predict(source=buffered, conf=0.5)result = results[0]draw = ImageDraw.Draw(image)download_allowed = Trueif result.boxes:for box in result.boxes:conf = float(box.conf)cls = int(box.cls)label = result.names.get(cls, "Unknown")x1, y1, x2, y2 = map(int, box.xyxy[0].tolist())color = "green" if conf >= threshold else "red"if conf < threshold:download_allowed = Falsedraw.rectangle([x1, y1, x2, y2], outline=color, width=4)draw.text((x1, max(0, y1 - 10)), f"{label} {conf:.2f}", fill=color)else:download_allowed = Falsest.warning("β Tidak ada objek yang terdeteksi.")# Tampilkan hasil akhirst.image(image, caption="Hasil Deteksi", use_column_width=True)# Tombol download jika confidence cukupif download_allowed:st.success("π’ Confidence β₯ 0.92: Serviks terdeteksi")output = BytesIO()image.save(output, format="JPEG")st.download_button("π₯ Download Gambar",data=output.getvalue(),file_name="deteksi_serviks.jpg",mime="image/jpeg")else:st.error("π΄ Confidence terlalu rendah atau tidak ada deteksi")
src/streamlit_app.py+87-38