Spaces:
No application file
No application file
Download app.py from Emma1030/streamlit2: direct link, hf CLI and curl.
- Browser
- Download file 1.98 kB
-
https://huggingface.co/spaces/Emma1030/streamlit2/resolve/main/app.py
- Command line
-
hf download hf://spaces/Emma1030/streamlit2/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Emma1030/streamlit2/resolve/main/app.py
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}") | |