fahad11182 commited on
Commit
e6bf3e3
1 Parent(s): d2e9188

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -16
app.py CHANGED
@@ -11,22 +11,32 @@ DetectorFactory.seed = 0
11
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
12
 
13
  def generate_response(message, system_message, max_tokens, temperature, top_p):
 
 
 
14
  messages = [{"role": "system", "content": system_message}]
15
  messages.append({"role": "user", "content": message})
16
 
17
  response = ""
18
- for message in client.chat_completion(
19
  messages,
20
  max_tokens=max_tokens,
21
  stream=True,
22
  temperature=temperature,
23
  top_p=top_p,
24
  ):
25
- token = message.choices[0].delta.content
26
  response += token
27
 
 
 
 
 
 
 
 
28
  # Generate speech from the response text using gTTS
29
- tts = gTTS(text=response, lang='en' if detect(message) == 'en' else 'ar')
30
  audio_file = "response.mp3"
31
  tts.save(audio_file)
32
  return response, audio_file
@@ -48,17 +58,18 @@ if st.button("Generate Response"):
48
  response, audio_file = generate_response(message, system_message, max_tokens, temperature, top_p)
49
  st.write(response)
50
 
51
- # Get base64 encoded audio data
52
- audio_base64 = get_base64_audio(audio_file)
53
-
54
- # HTML to autoplay the audio
55
- audio_html = f"""
56
- <audio controls autoplay>
57
- <source src="data:audio/mp3;base64,{audio_base64}" type="audio/mp3">
58
- Your browser does not support the audio element.
59
- </audio>
60
- """
61
- st.markdown(audio_html, unsafe_allow_html=True)
 
62
 
63
- # Clean up the audio file
64
- os.remove(audio_file)
 
11
  client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
12
 
13
  def generate_response(message, system_message, max_tokens, temperature, top_p):
14
+ if not message:
15
+ return "Please enter a message.", None
16
+
17
  messages = [{"role": "system", "content": system_message}]
18
  messages.append({"role": "user", "content": message})
19
 
20
  response = ""
21
+ for msg in client.chat_completion(
22
  messages,
23
  max_tokens=max_tokens,
24
  stream=True,
25
  temperature=temperature,
26
  top_p=top_p,
27
  ):
28
+ token = msg.choices[0].delta.content
29
  response += token
30
 
31
+ # Detect language and set TTS language
32
+ try:
33
+ lang = detect(message)
34
+ except Exception as e:
35
+ st.error(f"Error detecting language: {e}")
36
+ lang = 'en'
37
+
38
  # Generate speech from the response text using gTTS
39
+ tts = gTTS(text=response, lang='ar' if lang == 'ar' else 'en')
40
  audio_file = "response.mp3"
41
  tts.save(audio_file)
42
  return response, audio_file
 
58
  response, audio_file = generate_response(message, system_message, max_tokens, temperature, top_p)
59
  st.write(response)
60
 
61
+ if audio_file:
62
+ # Get base64 encoded audio data
63
+ audio_base64 = get_base64_audio(audio_file)
64
+
65
+ # HTML to autoplay the audio
66
+ audio_html = f"""
67
+ <audio controls autoplay>
68
+ <source src="data:audio/mp3;base64,{audio_base64}" type="audio/mp3">
69
+ Your browser does not support the audio element.
70
+ </audio>
71
+ """
72
+ st.markdown(audio_html, unsafe_allow_html=True)
73
 
74
+ # Clean up the audio file
75
+ os.remove(audio_file)