Santipab commited on
Commit
c136704
·
verified ·
1 Parent(s): d3d96bc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -0
app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from pydub import AudioSegment
3
+ from aift.multimodal import textqa
4
+ from aift import setting
5
+ import configparser
6
+ import requests
7
+ import json
8
+ import subprocess
9
+
10
+ setting.set_api_key('T69FqnYgOdreO5G0nZaM8gHcjo1sifyU')
11
+
12
+ st.write("# เลือกภาษาถิ่น")
13
+ land = ["< โปรดเลือกภาษาถิ่นของคุณ >", "อีสาน", "เหนือ", "ใต้"]
14
+ selected_region = st.selectbox("Select Region:", land, index=0)
15
+
16
+ region_ports = {
17
+ "อีสาน": 27020,
18
+ "เหนือ": 27021,
19
+ "ใต้": 27022
20
+ }
21
+
22
+ config = configparser.ConfigParser()
23
+ config.read("config.ini")
24
+
25
+ new_port = str(region_ports.get(selected_region, config["DEFAULT"].get("_server_port", "27021")))
26
+ config["DEFAULT"]["_server_port"] = new_port
27
+ config["SERVER"]["_server_port"] = new_port
28
+
29
+ with open("config.ini", "w") as configfile:
30
+ config.write(configfile)
31
+
32
+ audio_value = st.audio_input("Record a voice message")
33
+
34
+ if audio_value is not None:
35
+
36
+ st.audio(audio_value)
37
+ audio_bytes = audio_value.getvalue()
38
+ file_path = "./recorded_audio.wav"
39
+ with open(file_path, "wb") as file:
40
+ file.write(audio_bytes)
41
+ temp_file = "./recorded_audio.wav"
42
+ with open(temp_file, "wb") as file:
43
+ file.write(audio_bytes)
44
+
45
+ audio = AudioSegment.from_wav(temp_file)
46
+ audio = audio.set_frame_rate(16000).set_channels(1)
47
+ output_file = "recorded_audio.wav"
48
+ audio.export(output_file, format="wav")
49
+
50
+ st.success(f"Audio saved and modified as {output_file}")
51
+ # Run external process to get transcript
52
+ command = ["python", "partii-client-process-wav-file.py", output_file, "T69FqnYgOdreO5G0nZaM8gHcjo1sifyU"]
53
+ result = subprocess.run(command, capture_output=True, text=True)
54
+
55
+ output_list = [line for line in result.stdout.strip().split('\n') if line.startswith("transcript")]
56
+ if output_list:
57
+ speech_t = output_list[-1].replace("transcript", "").strip()
58
+ st.write(f"Transcript: {speech_t}")
59
+
60
+ # Generate answer using textqa
61
+ answer = textqa.generate(f'{selected_region} คำว่า "{speech_t}" แปลว่าอะไรภาษาไทย (ตอบแค่คำแปล)', return_json=False)
62
+
63
+ # Translate answer to English
64
+ url = "https://api.aiforthai.in.th/xiaofan-en-th/th2en"
65
+ payload = json.dumps({"text": answer})
66
+ headers = {
67
+ 'apikey': 'T69FqnYgOdreO5G0nZaM8gHcjo1sifyU',
68
+ 'Content-Type': 'application/json'
69
+ }
70
+ response = requests.post(url, headers=headers, data=payload)
71
+ Outt = eval(response.text)["translated_text"]
72
+ st.write(f'Translated Answer: {Outt}')
73
+ else:
74
+ st.error("Failed to extract transcript.")
75
+
76
+ else:
77
+ st.warning("Please select your dialect before recording.")
78
+