jdbolter commited on
Commit
d2475c1
·
verified ·
1 Parent(s): ad68cb4

fixed the history feature for the prompts

Browse files
Files changed (1) hide show
  1. app.py +41 -17
app.py CHANGED
@@ -1,3 +1,11 @@
 
 
 
 
 
 
 
 
1
  import os
2
  import base64
3
  import uuid
@@ -10,6 +18,7 @@ from dotenv import load_dotenv
10
  RUNNING_IN_SPACES = os.getenv("SYSTEM") == "spaces"
11
 
12
  # Load API keys
 
13
  if not RUNNING_IN_SPACES:
14
  load_dotenv()
15
  openai_api_key = os.getenv("OPENAI_API_KEY")
@@ -45,34 +54,41 @@ language_config = {
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}")
57
 
58
- # Step 1: Get GPT response
59
  system_message = f"You are a friendly {language_choice} language tutor. Respond only in {language_choice}."
 
 
 
 
 
 
 
 
60
  completion = openai_client.chat.completions.create(
61
  model="gpt-4",
62
- messages=[
63
- {"role": "system", "content": system_message},
64
- {"role": "user", "content": user_input}
65
- ]
66
  )
67
  gpt_response = completion.choices[0].message.content
68
  print(f"💬 GPT response: {gpt_response}")
69
 
70
- # Step 2: Use Speechify to generate audio
71
  config = language_config.get(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,
@@ -91,24 +107,30 @@ 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 None, f"{gpt_response}\n\n⚠️ Error saving audio: {audio_err}"
95
  else:
96
  print("⚠️ No audio data received from Speechify or audio_data is not a string.")
97
- return None, f"{gpt_response}\n\n⚠️ No audio data received from Speechify."
98
 
99
- return audio_output_path, gpt_response
 
 
 
 
100
 
101
  except Exception as e:
 
102
  print(f"🔥 An unexpected error occurred: {e}")
103
  error_message = f"⚠️ An unexpected error occurred: {e}"
104
  if gpt_response:
105
- return None, f"{gpt_response}\n\n{error_message}"
106
- return None, error_message
107
 
 
108
  with open("custom.css") as f:
109
  custom_css = f.read()
110
 
111
-
112
  with gr.Blocks(css=custom_css) as demo:
113
  gr.HTML(
114
  '<div class="custom-bar"><span class="custom-bar-title">Language Tutor</span></div>'
@@ -123,15 +145,17 @@ with gr.Blocks(css=custom_css) as demo:
123
  label="Language"
124
  )
125
  submit_btn = gr.Button("Submit")
 
126
  with gr.Column():
127
  audio_output = gr.Audio(label="Audio Playback", type="filepath", autoplay=True)
128
  gpt_output = gr.Textbox(label="The Response")
129
 
130
  submit_btn.click(
131
  fn=chat_and_speak,
132
- inputs=[user_input, language_choice],
133
- outputs=[audio_output, gpt_output]
134
  )
135
 
 
136
  if __name__ == "__main__":
137
  demo.launch()
 
1
+ """Language Tutor Application
2
+
3
+ This script provides a Gradio-based web interface for a language tutoring assistant.
4
+ It uses OpenAI's GPT-4 model to generate language-specific responses and Speechify's
5
+ text-to-speech service to synthesize audio in multiple languages (Portuguese, French, Spanish).
6
+ The application supports running both locally and in Hugging Face Spaces environments.
7
+ """
8
+
9
  import os
10
  import base64
11
  import uuid
 
18
  RUNNING_IN_SPACES = os.getenv("SYSTEM") == "spaces"
19
 
20
  # Load API keys
21
+ # Load environment variables from .env when not running in Spaces
22
  if not RUNNING_IN_SPACES:
23
  load_dotenv()
24
  openai_api_key = os.getenv("OPENAI_API_KEY")
 
54
  },
55
  }
56
 
57
+ def chat_and_speak(user_input, language_choice, history):
58
+ # Step 0: Initialize response variables
59
  gpt_response = ""
60
  audio_output_path = None
61
  try:
62
+ # Step 1: Input validation
63
  if not user_input or not user_input.strip():
64
+ return None, "Please enter some text to process.", history
65
 
66
  print(f"🧠 User input: {user_input}")
67
  print(f"🗣️ Language choice: {language_choice}")
68
 
69
+ # Build messages with history for GPT interaction
70
  system_message = f"You are a friendly {language_choice} language tutor. Respond only in {language_choice}."
71
+ messages = [{"role": "system", "content": system_message}]
72
+ if history:
73
+ for user_msg, assistant_msg in history:
74
+ messages.append({"role": "user", "content": user_msg})
75
+ messages.append({"role": "assistant", "content": assistant_msg})
76
+ messages.append({"role": "user", "content": user_input})
77
+
78
+ # Step 2: GPT interaction to generate response
79
  completion = openai_client.chat.completions.create(
80
  model="gpt-4",
81
+ messages=messages
 
 
 
82
  )
83
  gpt_response = completion.choices[0].message.content
84
  print(f"💬 GPT response: {gpt_response}")
85
 
86
+ # Step 3: Voice synthesis using Speechify
87
  config = language_config.get(language_choice)
88
  if not config:
89
  error_msg = f"⚠️ Language '{language_choice}' not supported."
90
  print(error_msg)
91
+ return None, f"{gpt_response}\n\n{error_msg}", history
92
 
93
  tts_response = speechify_client.tts.audio.speech(
94
  input=gpt_response,
 
107
  f.write(audio_bytes)
108
  except Exception as audio_err:
109
  print(f"🔥 Error processing audio data: {audio_err}")
110
+ return None, f"{gpt_response}\n\n⚠️ Error saving audio: {audio_err}", history
111
  else:
112
  print("⚠️ No audio data received from Speechify or audio_data is not a string.")
113
+ return None, f"{gpt_response}\n\n⚠️ No audio data received from Speechify.", history
114
 
115
+ # Append new interaction to history
116
+ history = history or []
117
+ history.append((user_input, gpt_response))
118
+
119
+ return audio_output_path, gpt_response, history
120
 
121
  except Exception as e:
122
+ # Step 4: Error handling
123
  print(f"🔥 An unexpected error occurred: {e}")
124
  error_message = f"⚠️ An unexpected error occurred: {e}"
125
  if gpt_response:
126
+ return None, f"{gpt_response}\n\n{error_message}", history
127
+ return None, error_message, history
128
 
129
+ # Load custom CSS for UI styling
130
  with open("custom.css") as f:
131
  custom_css = f.read()
132
 
133
+ # Define Gradio UI layout
134
  with gr.Blocks(css=custom_css) as demo:
135
  gr.HTML(
136
  '<div class="custom-bar"><span class="custom-bar-title">Language Tutor</span></div>'
 
145
  label="Language"
146
  )
147
  submit_btn = gr.Button("Submit")
148
+ chat_history = gr.State([])
149
  with gr.Column():
150
  audio_output = gr.Audio(label="Audio Playback", type="filepath", autoplay=True)
151
  gpt_output = gr.Textbox(label="The Response")
152
 
153
  submit_btn.click(
154
  fn=chat_and_speak,
155
+ inputs=[user_input, language_choice, chat_history],
156
+ outputs=[audio_output, gpt_output, chat_history]
157
  )
158
 
159
+ # Launch the Gradio app
160
  if __name__ == "__main__":
161
  demo.launch()