jdbolter commited on
Commit
f1a9277
·
verified ·
1 Parent(s): 96e3d85

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +35 -23
  2. custom.css +32 -0
app.py CHANGED
@@ -45,13 +45,12 @@ language_config = {
45
  },
46
  }
47
 
48
- # Function for chat + TTS
49
  def chat_and_speak(user_input, language_choice):
50
  gpt_response = ""
51
  audio_output_path = None
52
  try:
53
  if not user_input or not user_input.strip():
54
- return "Please enter some text to process.", None
55
 
56
  print(f"🧠 User input: {user_input}")
57
  print(f"🗣️ Language choice: {language_choice}")
@@ -73,7 +72,7 @@ def chat_and_speak(user_input, language_choice):
73
  if not config:
74
  error_msg = f"⚠️ Language '{language_choice}' not supported."
75
  print(error_msg)
76
- return f"{gpt_response}\n\n{error_msg}", None
77
 
78
  tts_response = speechify_client.tts.audio.speech(
79
  input=gpt_response,
@@ -91,10 +90,10 @@ def chat_and_speak(user_input, language_choice):
91
  f.write(audio_bytes)
92
  except Exception as audio_err:
93
  print(f"🔥 Error processing audio data: {audio_err}")
94
- return f"{gpt_response}\n\n⚠️ Error saving audio: {audio_err}", None
95
  else:
96
  print("⚠️ No audio data received from Speechify or audio_data is not a string.")
97
- return f"{gpt_response}\n\n⚠️ No audio data received from Speechify.", None
98
 
99
  return audio_output_path, gpt_response
100
 
@@ -102,23 +101,36 @@ def chat_and_speak(user_input, language_choice):
102
  print(f"🔥 An unexpected error occurred: {e}")
103
  error_message = f"⚠️ An unexpected error occurred: {e}"
104
  if gpt_response:
105
- return f"{gpt_response}\n\n{error_message}", None
106
- return error_message, None
107
-
108
- # Gradio interface
109
- iface = gr.Interface(
110
- fn=chat_and_speak,
111
- inputs=[
112
- gr.Textbox(label="Say something"),
113
- gr.Dropdown(choices=["Portuguese", "French", "Spanish"], value="Portuguese", label="Language"),
114
- ],
115
- outputs=[
116
- gr.Audio(label="TTS Playback", type="filepath", autoplay=True),
117
- gr.Textbox(label="GPT Response")
118
- ],
119
- title="Language Tutor with GPT and Speechify",
120
- allow_flagging="never"
121
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
122
 
123
  if __name__ == "__main__":
124
- iface.launch()
 
45
  },
46
  }
47
 
 
48
  def chat_and_speak(user_input, language_choice):
49
  gpt_response = ""
50
  audio_output_path = None
51
  try:
52
  if not user_input or not user_input.strip():
53
+ return None, "Please enter some text to process."
54
 
55
  print(f"🧠 User input: {user_input}")
56
  print(f"🗣️ Language choice: {language_choice}")
 
72
  if not config:
73
  error_msg = f"⚠️ Language '{language_choice}' not supported."
74
  print(error_msg)
75
+ return None, f"{gpt_response}\n\n{error_msg}"
76
 
77
  tts_response = speechify_client.tts.audio.speech(
78
  input=gpt_response,
 
90
  f.write(audio_bytes)
91
  except Exception as audio_err:
92
  print(f"🔥 Error processing audio data: {audio_err}")
93
+ return None, f"{gpt_response}\n\n⚠️ Error saving audio: {audio_err}"
94
  else:
95
  print("⚠️ No audio data received from Speechify or audio_data is not a string.")
96
+ return None, f"{gpt_response}\n\n⚠️ No audio data received from Speechify."
97
 
98
  return audio_output_path, gpt_response
99
 
 
101
  print(f"🔥 An unexpected error occurred: {e}")
102
  error_message = f"⚠️ An unexpected error occurred: {e}"
103
  if gpt_response:
104
+ return None, f"{gpt_response}\n\n{error_message}"
105
+ return None, error_message
106
+
107
+ with open("custom.css") as f:
108
+ custom_css = f.read()
109
+
110
+
111
+ with gr.Blocks(css=custom_css) as demo:
112
+ gr.HTML(
113
+ '<div class="custom-bar"><span class="custom-bar-title">Speaking Language Tutor</span></div>'
114
+ )
115
+ with gr.Column(elem_classes="main-card"):
116
+ with gr.Row():
117
+ with gr.Column():
118
+ user_input = gr.Textbox(label="Type in whatever language you want to practice", placeholder="Type here...", lines=4)
119
+ language_choice = gr.Dropdown(
120
+ choices=["Portuguese", "French", "Spanish"],
121
+ value="Portuguese",
122
+ label="Language"
123
+ )
124
+ submit_btn = gr.Button("Submit")
125
+ with gr.Column():
126
+ audio_output = gr.Audio(label="Audio Playback", type="filepath", autoplay=True)
127
+ gpt_output = gr.Textbox(label="The Response")
128
+
129
+ submit_btn.click(
130
+ fn=chat_and_speak,
131
+ inputs=[user_input, language_choice],
132
+ outputs=[audio_output, gpt_output]
133
+ )
134
 
135
  if __name__ == "__main__":
136
+ demo.launch()
custom.css ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ body, .gradio-container {
2
+ background-color: #f7f7fa !important;
3
+ }
4
+ p { margin: 0; }
5
+ .custom-bar {
6
+ width: 100%;
7
+ height: 48px;
8
+ background: linear-gradient(90deg, #4f8cff 0%, #a0e9ff 100%);
9
+ margin-bottom: 32px;
10
+ border-radius: 8px;
11
+ display: flex;
12
+ align-items: center; /* Vertically center content */
13
+ justify-content: center; /* Horizontally center content */
14
+ padding: 0 16px; /* Optional: horizontal padding */
15
+ box-sizing: border-box; /* Ensure padding doesn't affect height */
16
+ }
17
+ .custom-bar-title {
18
+ color: #fff;
19
+ font-size: 1.6rem;
20
+ font-weight: 700;
21
+ letter-spacing: 1px;
22
+ margin: 0;
23
+ line-height: 1; /* Prevents extra space above/below */
24
+ }
25
+ .main-card {
26
+ background: #fff;
27
+ border-radius: 16px;
28
+ box-shadow: 0 4px 24px rgba(0,0,0,0.07);
29
+ padding: 32px;
30
+ max-width: 700px;
31
+ margin: 0 auto 32px auto;
32
+ }