Spaces:
No application file
No application file
File size: 1,982 Bytes
f163fb7 e54e072 2cc00c9 f163fb7 2cc00c9 f163fb7 2cc00c9 2bea5ce 2cc00c9 f163fb7 2cc00c9 e54e072 2cc00c9 e54e072 2cc00c9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | 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}")
|