streamlit2 / app.py
Emma1030's picture
Rename streamlit_app.py to app.py
0a89481 verified
Raw History Blame Contribute Delete
1.98 kB
import streamlit as st
import requests
import base64
import sounddevice as sd
import soundfile as sf
import tempfile
# โœ… ๆ›ฟๆ›ๆˆไฝ ็š„ ngrok ๆˆ– render ๅพŒ็ซฏ
API_BASE = "https://f876-125-228-143-171.ngrok-free.app "
st.set_page_config(page_title="๐Ÿ“‡ ๅ็‰‡่พจ่ญ˜่ˆ‡่ชž้Ÿณ่ผธๅ…ฅ", layout="centered")
st.title("๐Ÿ“ท ๆ‹็…ง + ๐ŸŽ™ ่ชž้Ÿณ่พจ่ญ˜็ณป็ตฑ")
st.header("๐Ÿ“ธ ๅ็‰‡ๆ‹็…ง่พจ่ญ˜")
img_file = st.camera_input("่ซ‹ๆ‹ๆ”ๅ็‰‡")
if img_file:
st.image(img_file, caption="ๅ็‰‡้ ่ฆฝ", use_column_width=True)
img_bytes = img_file.getvalue()
base64_img = base64.b64encode(img_bytes).decode("utf-8")
payload = {"image": f"data:image/jpeg;base64,{base64_img}"}
with st.spinner("๐Ÿง  OCR ่™•็†ไธญ..."):
try:
res = requests.post(f"{API_BASE}/ocr", json=payload)
text = res.json().get("text", "")
st.text_area("๐Ÿ“„ ่พจ่ญ˜็ตๆžœ", value=text, height=200)
except Exception as e:
st.error(f"โŒ ้Œฏ่ชค๏ผš{e}")
st.header("๐ŸŽค ่ชž้Ÿณ่พจ่ญ˜๏ผˆWhisper๏ผ‰")
duration = st.slider("โฑ ้Œ„้Ÿณ็ง’ๆ•ธ", 2, 10, 4)
if st.button("๐ŸŽ™ ้–‹ๅง‹้Œ„้Ÿณ"):
with st.spinner("๐ŸŽง ้Œ„้Ÿณไธญ..."):
fs = 16000
audio = sd.rec(int(duration * fs), samplerate=fs, channels=1)
sd.wait()
with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as tmp:
sf.write(tmp.name, audio, fs)
tmp.seek(0)
audio_bytes = tmp.read()
st.audio(audio_bytes, format="audio/wav")
with st.spinner("๐Ÿง  Whisper ่™•็†ไธญ..."):
try:
res = requests.post(
f"{API_BASE}/whisper",
files={"file": ("audio.wav", audio_bytes, "audio/wav")}
)
result = res.json().get("text", "")
st.text_area("๐Ÿ“ ่ชž้Ÿณ่พจ่ญ˜็ตๆžœ", value=result, height=200)
except Exception as e:
st.error(f"โŒ Whisper API ้Œฏ่ชค๏ผš{e}")