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}")