ibrahim313 commited on
Commit
d7a9f29
·
verified ·
1 Parent(s): 30218c0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +89 -99
app.py CHANGED
@@ -1,101 +1,91 @@
1
- import os
2
  import streamlit as st
3
- import pandas as pd
4
- from groq import Groq
5
- import sounddevice as sd
6
  import numpy as np
7
- import wavio
8
-
9
- # Set your API key (replace with secure method for production)
10
- API_KEY = "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"
11
-
12
- # Initialize Groq client
13
- client = Groq(api_key=API_KEY)
14
-
15
- # Streamlit app layout
16
- st.set_page_config(page_title="PhoneX: Personalized Reading App", layout="wide")
17
- st.title("Welcome to PhoneX: Your Personalized Reading App")
18
- st.sidebar.title("Navigation")
19
-
20
- # User Registration
21
- user_id = st.sidebar.text_input("Enter your User ID:")
22
- if st.sidebar.button("Register"):
23
- if user_id:
24
- st.sidebar.success(f"User {user_id} registered successfully!")
25
- else:
26
- st.sidebar.error("Please enter a valid User ID.")
27
-
28
- # Diagnostic Test
29
- st.header("Diagnostic Test")
30
- assessment_text = st.text_area("Read the following text aloud:", "Sample text for assessment: The quick brown fox jumps over the lazy dog.")
31
- if st.button("Submit Assessment"):
32
- if assessment_text:
33
- # Call the LLAMA model for feedback based on assessment
34
- chat_completion = client.chat.completions.create(
35
- messages=[
36
- {
37
- "role": "user",
38
- "content": f"Evaluate the following assessment: {assessment_text}",
39
- }
40
- ],
41
- model="llama3-8b-8192",
42
- )
43
- st.success("Assessment submitted successfully!")
44
- st.write("LLAMA Model Output:", chat_completion.choices[0].message.content)
45
- else:
46
- st.error("Please enter the text to assess.")
47
-
48
- # Personalized Reading Material
49
- st.header("Generate Personalized Reading Material")
50
- if st.button("Generate Reading Material"):
51
- generated_text = "Generated personalized text focusing on phonetic patterns: 'sh', 'ch', and 'th'." # Placeholder for personalized content
52
- st.text_area("Your Personalized Reading Material:", generated_text, height=200)
53
-
54
- # Audio Recording Feature
55
- st.header("Record Your Voice")
56
- duration = st.number_input("Duration of recording (seconds)", min_value=1, max_value=60, value=5)
57
- if st.button("Record Audio"):
58
- st.write("Recording...")
59
- recording = sd.rec(int(duration * 44100), samplerate=44100, channels=2)
60
- sd.wait() # Wait until the recording is finished
61
- wavio.write("recording.wav", recording, 44100, sampwidth=2) # Save as WAV file
62
- st.success("Recording completed!")
63
-
64
- # Upload Recorded Audio for Transcription
65
- if st.file_uploader("Upload your audio file (m4a):", type=["m4a"]) is not None:
66
- if st.button("Transcribe Audio"):
67
- filename = "recording.wav" # Use the recorded file for transcription
68
- with open(filename, "rb") as file:
69
- transcription = client.audio.transcriptions.create(
70
- file=(filename, file.read()),
71
- model="whisper-large-v3",
72
- response_format="verbose_json",
73
- )
74
- st.write("Transcription:", transcription.text)
75
-
76
- # Feedback Collection
77
- st.header("Feedback Collection")
78
- feedback = st.text_area("Your Feedback:")
79
- if st.button("Submit Feedback"):
80
- if feedback:
81
- # Placeholder for storing feedback (e.g., to Google Sheets)
82
- st.success("Feedback submitted successfully!")
83
- else:
84
- st.error("Please provide your feedback.")
85
-
86
- # Visualization (User Progress)
87
- st.header("User Progress Visualization")
88
- # Placeholder for progress data visualization
89
- progress_data = pd.DataFrame({'Task': ['Reading', 'Feedback'], 'Scores': [85, 90]})
90
- st.bar_chart(progress_data.set_index('Task'))
91
-
92
- # Error Handling Example
93
- try:
94
- # Your main logic here
95
- pass
96
- except Exception as e:
97
- st.error(f"An error occurred: {e}")
98
-
99
- # Footer
100
- st.sidebar.markdown("---")
101
- st.sidebar.markdown("Created by Your Name")
 
 
1
  import streamlit as st
2
+ from transformers import Wav2Vec2ForCTC, Wav2Vec2Processor
3
+ import torch
 
4
  import numpy as np
5
+ import tempfile
6
+ import wave
7
+
8
+ # Load Wav2Vec2 model and processor
9
+ processor = Wav2Vec2Processor.from_pretrained("facebook/wav2vec2-base-960h")
10
+ model = Wav2Vec2ForCTC.from_pretrained("facebook/wav2vec2-base-960h")
11
+
12
+ # Streamlit App
13
+ st.title("Phonics/Personalized Reading App")
14
+ st.write("Record your audio and we will transcribe it.")
15
+
16
+ # Audio recording using HTML5
17
+ record_button = st.button("Record Audio")
18
+
19
+ if record_button:
20
+ st.markdown("""
21
+ <audio id="audio" controls></audio>
22
+ <button id="start" onclick="startRecording()">Start Recording</button>
23
+ <button id="stop" onclick="stopRecording()" disabled>Stop Recording</button>
24
+ <script>
25
+ let mediaRecorder;
26
+ let audioChunks = [];
27
+
28
+ async function startRecording() {
29
+ const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
30
+ mediaRecorder = new MediaRecorder(stream);
31
+ mediaRecorder.ondataavailable = event => {
32
+ audioChunks.push(event.data);
33
+ };
34
+
35
+ mediaRecorder.onstop = () => {
36
+ const audioBlob = new Blob(audioChunks, { type: 'audio/wav' });
37
+ const audioUrl = URL.createObjectURL(audioBlob);
38
+ const audioElement = document.getElementById('audio');
39
+ audioElement.src = audioUrl;
40
+
41
+ // Prepare to send audio to server
42
+ const formData = new FormData();
43
+ formData.append('audio', audioBlob, 'recording.wav');
44
+
45
+ fetch('/upload', {
46
+ method: 'POST',
47
+ body: formData
48
+ }).then(response => response.json()).then(data => {
49
+ st.session_state.transcription = data.transcription;
50
+ st.experimental_rerun(); // Refresh the app to show the transcription
51
+ });
52
+ };
53
+
54
+ mediaRecorder.start();
55
+ document.getElementById('start').disabled = true;
56
+ document.getElementById('stop').disabled = false;
57
+ }
58
+
59
+ function stopRecording() {
60
+ mediaRecorder.stop();
61
+ document.getElementById('start').disabled = false;
62
+ document.getElementById('stop').disabled = true;
63
+ }
64
+ </script>
65
+ """, unsafe_allow_html=True)
66
+
67
+ # Display the transcription
68
+ if 'transcription' in st.session_state:
69
+ st.write("Transcription:")
70
+ st.write(st.session_state.transcription)
71
+
72
+ # Handle audio file upload
73
+ uploaded_file = st.file_uploader("Or upload your audio file", type=["wav", "mp3"])
74
+
75
+ if uploaded_file is not None:
76
+ # Save uploaded audio file to a temporary file
77
+ with tempfile.NamedTemporaryFile(delete=True) as temp_file:
78
+ temp_file.write(uploaded_file.read())
79
+ temp_file.flush()
80
+
81
+ # Process the audio file for transcription
82
+ audio_input = processor(temp_file.name, sampling_rate=16000, return_tensors="pt", padding=True)
83
+
84
+ with torch.no_grad():
85
+ logits = model(audio_input.input_values).logits
86
+
87
+ predicted_ids = torch.argmax(logits, dim=-1)
88
+ transcription = processor.batch_decode(predicted_ids)
89
+
90
+ st.session_state.transcription = transcription[0] # Store transcription
91
+ st.experimental_rerun() # Refresh the app to show the transcription