Spaces:
Sleeping
Sleeping
File size: 2,833 Bytes
c136704 |
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
import streamlit as st
from pydub import AudioSegment
from aift.multimodal import textqa
from aift import setting
import configparser
import requests
import json
import subprocess
setting.set_api_key('T69FqnYgOdreO5G0nZaM8gHcjo1sifyU')
st.write("# เลือกภาษาถิ่น")
land = ["< โปรดเลือกภาษาถิ่นของคุณ >", "อีสาน", "เหนือ", "ใต้"]
selected_region = st.selectbox("Select Region:", land, index=0)
region_ports = {
"อีสาน": 27020,
"เหนือ": 27021,
"ใต้": 27022
}
config = configparser.ConfigParser()
config.read("config.ini")
new_port = str(region_ports.get(selected_region, config["DEFAULT"].get("_server_port", "27021")))
config["DEFAULT"]["_server_port"] = new_port
config["SERVER"]["_server_port"] = new_port
with open("config.ini", "w") as configfile:
config.write(configfile)
audio_value = st.audio_input("Record a voice message")
if audio_value is not None:
st.audio(audio_value)
audio_bytes = audio_value.getvalue()
file_path = "./recorded_audio.wav"
with open(file_path, "wb") as file:
file.write(audio_bytes)
temp_file = "./recorded_audio.wav"
with open(temp_file, "wb") as file:
file.write(audio_bytes)
audio = AudioSegment.from_wav(temp_file)
audio = audio.set_frame_rate(16000).set_channels(1)
output_file = "recorded_audio.wav"
audio.export(output_file, format="wav")
st.success(f"Audio saved and modified as {output_file}")
# Run external process to get transcript
command = ["python", "partii-client-process-wav-file.py", output_file, "T69FqnYgOdreO5G0nZaM8gHcjo1sifyU"]
result = subprocess.run(command, capture_output=True, text=True)
output_list = [line for line in result.stdout.strip().split('\n') if line.startswith("transcript")]
if output_list:
speech_t = output_list[-1].replace("transcript", "").strip()
st.write(f"Transcript: {speech_t}")
# Generate answer using textqa
answer = textqa.generate(f'{selected_region} คำว่า "{speech_t}" แปลว่าอะไรภาษาไทย (ตอบแค่คำแปล)', return_json=False)
# Translate answer to English
url = "https://api.aiforthai.in.th/xiaofan-en-th/th2en"
payload = json.dumps({"text": answer})
headers = {
'apikey': 'T69FqnYgOdreO5G0nZaM8gHcjo1sifyU',
'Content-Type': 'application/json'
}
response = requests.post(url, headers=headers, data=payload)
Outt = eval(response.text)["translated_text"]
st.write(f'Translated Answer: {Outt}')
else:
st.error("Failed to extract transcript.")
else:
st.warning("Please select your dialect before recording.")
|