jonghhhh commited on
Commit
3455e4c
Β·
verified Β·
1 Parent(s): a860242

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -40
app.py CHANGED
@@ -1,7 +1,8 @@
1
  import streamlit as st
2
  from gtts import gTTS
3
  from pydub import AudioSegment
4
- import speech_recognition as sr
 
5
  import os
6
 
7
  # 학생 리슀트
@@ -13,48 +14,42 @@ absentees = []
13
  # Streamlit UI
14
  st.title("μΆœμ„ 확인 μ‹œμŠ€ν…œ")
15
 
16
- # μ˜€λ””μ˜€ 파일 생성 ν•¨μˆ˜
17
- def create_combined_audio_file(students):
18
- combined = AudioSegment.silent(duration=0) # μ‹œμž‘μ— 짧은 무음 μΆ”κ°€
19
- for student in students:
20
- tts = gTTS(student + " 학생")
21
- tts.save(f"{student}.mp3")
22
- audio = AudioSegment.from_mp3(f"{student}.mp3")
23
- combined += audio + AudioSegment.silent(duration=500) # 학생 이름 사이에 0.5초 간격 μΆ”κ°€
24
- combined.export("combined_students.wav", format="wav") # WAV ν˜•μ‹μœΌλ‘œ μ €μž₯
25
- return "combined_students.wav"
26
-
27
- # μ˜€λ””μ˜€ 파일 생성
28
- audio_file = create_combined_audio_file(student_list)
29
-
30
- # μŒμ„± 인식을 μœ„ν•œ ν•¨μˆ˜
31
- def recognize_audio(file):
32
- recognizer = sr.Recognizer()
33
- with sr.AudioFile(file) as source:
34
- audio = recognizer.record(source)
35
- try:
36
- return recognizer.recognize_google(audio, language="ko-KR")
37
- except sr.UnknownValueError:
38
- return "인식 λΆˆκ°€"
39
- except sr.RequestError:
40
- return "STT μ„œλΉ„μŠ€ μš”μ²­ μ‹€νŒ¨"
41
 
42
- # μ˜€λ””μ˜€ μž¬μƒ 및 μŒμ„± 인식 μ‹€ν–‰
43
- if st.button("전체 μ˜€λ””μ˜€ μž¬μƒ"):
44
- st.audio(audio_file)
45
- recognized_text = recognize_audio(audio_file)
46
- st.subheader("호λͺ…λœ 학생 이름")
47
- st.write(recognized_text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
 
49
  # 결석 처리
50
- if recognized_text:
51
- selected_student = st.selectbox("κ²°μ„μž 선택", options=recognized_text.split())
52
-
53
- if st.button("κ²°μ„μžλ‘œ 등둝"):
54
- absentees.append(selected_student)
55
- with open("absentees.txt", "a") as f:
56
- f.write(f"{selected_student}\n")
57
- st.success(f"{selected_student}κ°€ κ²°μ„μžλ‘œ λ“±λ‘λ˜μ—ˆμŠ΅λ‹ˆλ‹€.")
 
 
 
58
 
59
  # κ²°μ„μž 리슀트 보여주기
60
  st.subheader("κ²°μ„μž 리슀트")
 
1
  import streamlit as st
2
  from gtts import gTTS
3
  from pydub import AudioSegment
4
+ from pydub.playback import play
5
+ import tempfile
6
  import os
7
 
8
  # 학생 리슀트
 
14
  # Streamlit UI
15
  st.title("μΆœμ„ 확인 μ‹œμŠ€ν…œ")
16
 
17
+ if 'current_index' not in st.session_state:
18
+ st.session_state.current_index = 0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
 
20
+ # μ˜€λ””μ˜€ 파일 생성 ν•¨μˆ˜
21
+ def create_audio_for_student(student):
22
+ tts = gTTS(student + " 학생")
23
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.mp3')
24
+ tts.save(temp_file.name)
25
+ return temp_file.name
26
+
27
+ # ν˜„μž¬ 학생 이름 κ°€μ Έμ˜€κΈ°
28
+ current_student = student_list[st.session_state.current_index]
29
+
30
+ # 학생 이름 뢈러였기
31
+ if st.button("λ‹€μŒ 학생"):
32
+ # ν˜„μž¬ ν•™μƒμ˜ 이름을 μ˜€λ””μ˜€λ‘œ μž¬μƒ
33
+ audio_file = create_audio_for_student(current_student)
34
+ audio = AudioSegment.from_mp3(audio_file)
35
+ play(audio)
36
+ os.remove(audio_file) # μž„μ‹œ 파일 μ‚­μ œ
37
+
38
+ # 화면에 ν˜„μž¬ 학생 이름 ν‘œμ‹œ
39
+ st.subheader(f"ν˜„μž¬ 학생: {current_student}")
40
 
41
  # 결석 처리
42
+ if st.radio(f"{current_student} μΆœμ„ μ—¬λΆ€", ("μΆœμ„", "결석")) == "결석":
43
+ absentees.append(current_student)
44
+ with open("absentees.txt", "a") as f:
45
+ f.write(f"{current_student}\n")
46
+ st.success(f"{current_student}κ°€ κ²°μ„μžλ‘œ λ“±λ‘λ˜μ—ˆμŠ΅λ‹ˆλ‹€.")
47
+
48
+ # λ‹€μŒ ν•™μƒμœΌλ‘œ 인덱슀 이동
49
+ if st.session_state.current_index < len(student_list) - 1:
50
+ st.session_state.current_index += 1
51
+ else:
52
+ st.write("λͺ¨λ“  ν•™μƒμ˜ 이름을 λΆˆλ €μŠ΅λ‹ˆλ‹€.")
53
 
54
  # κ²°μ„μž 리슀트 보여주기
55
  st.subheader("κ²°μ„μž 리슀트")