AlexMia commited on
Commit
c330ab7
1 Parent(s): 7bd7554

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -14
app.py CHANGED
@@ -7,18 +7,25 @@ import urllib.parse
7
  import gradio as gr
8
  from groq import Groq
9
 
 
 
 
10
 
11
- def audio_response(text, voice="Sophia"): # Alex
12
- GET_AUDIO_API = os.environ.get("GET_AUDIO_API")
 
 
13
  url = f"https://{GET_AUDIO_API}/get-audio?text={urllib.parse.quote(text)}&voice={voice.lower()}"
14
  try:
15
  response = requests.get(url)
16
- return gr.Audio("response.content")
17
  except Exception as e:
18
- return f"Ein Fehler mit der GET_AUDIO_API ist aufgetreten: {e}", ""
19
-
20
 
21
  def bot(chat_msg, history=None, audio_response=False):
 
 
 
22
  messages = []
23
  if history is None:
24
  if audio_response:
@@ -29,8 +36,7 @@ def bot(chat_msg, history=None, audio_response=False):
29
  messages.append({"role": "user", "content": chat_msg})
30
 
31
  try:
32
- GROQ_API_KEY = os.environ.get("GROQ_API_KEY_AI_ECHO_BOT")
33
- client = Groq(api_key=GROQ_API_KEY)
34
  completion = client.chat.completions.create(
35
  model="llama3-8b-8192",
36
  messages=messages,
@@ -43,19 +49,19 @@ def bot(chat_msg, history=None, audio_response=False):
43
  bot_message = completion.choices[0].message.content
44
  if audio_response:
45
  response_audio = audio_response(bot_message, voice="Sophia")
46
- time.sleep(1)
47
- return "", response_audio if not "Ein Fehler mit der GET_AUDIO_API" in response_audio else bot_message, ""
48
  except Exception as e:
49
  bot_message = f"Fehler: {e}"
 
50
  return "", bot_message, ""
51
 
52
-
53
  with gr.Blocks() as demo:
54
  chatbot = gr.Chatbot(type="messages")
55
  chat_input = gr.Textbox(placeholder="Sende eine Nachricht...", lines=1, max_lines=6, show_label=False, elem_id="chat_input")
56
- audio_response = gr.Checkbox(label="Audio Antwort generieren?", value=False)
57
- clear = gr.ClearButton([chat_input, chatbot, audio_response], show_api=False)
58
 
59
- chat_input.submit(bot, [chat_input, chatbot, audio_response], [chat_input, chatbot, audio_response], api_name="llm_inference")
60
 
61
- demo.launch()
 
7
  import gradio as gr
8
  from groq import Groq
9
 
10
+ # Konfiguration
11
+ GET_AUDIO_API = os.environ.get("GET_AUDIO_API")
12
+ GROQ_API_KEY_AI_ECHO_BOT = os.environ.get("GROQ_API_KEY_AI_ECHO_BOT")
13
 
14
+ def audio_response(text, voice="Sophia"):
15
+ """
16
+ Holt die Audio-Antwort von der API.
17
+ """
18
  url = f"https://{GET_AUDIO_API}/get-audio?text={urllib.parse.quote(text)}&voice={voice.lower()}"
19
  try:
20
  response = requests.get(url)
21
+ return gr.Audio(response.content)
22
  except Exception as e:
23
+ return f"Ein Fehler mit der GET_AUDIO_API ist aufgetreten: {e}"
 
24
 
25
  def bot(chat_msg, history=None, audio_response=False):
26
+ """
27
+ Prozessiert die Benutzereingabe und gibt eine Antwort zurück.
28
+ """
29
  messages = []
30
  if history is None:
31
  if audio_response:
 
36
  messages.append({"role": "user", "content": chat_msg})
37
 
38
  try:
39
+ client = Groq(api_key=GROQ_API_KEY_AI_ECHO_BOT)
 
40
  completion = client.chat.completions.create(
41
  model="llama3-8b-8192",
42
  messages=messages,
 
49
  bot_message = completion.choices[0].message.content
50
  if audio_response:
51
  response_audio = audio_response(bot_message, voice="Sophia")
52
+ messages.append({"role": "assistant", content=response_audio if not "Ein Fehler mit der GET_AUDIO_API" in response_audio else bot_message})
53
+ return "", messages, ""
54
  except Exception as e:
55
  bot_message = f"Fehler: {e}"
56
+ messages.append({"role": "assistant", content=bot_message})
57
  return "", bot_message, ""
58
 
 
59
  with gr.Blocks() as demo:
60
  chatbot = gr.Chatbot(type="messages")
61
  chat_input = gr.Textbox(placeholder="Sende eine Nachricht...", lines=1, max_lines=6, show_label=False, elem_id="chat_input")
62
+ audio_response_checkbox = gr.Checkbox(label="Audio Antwort generieren?", value=False)
63
+ clear = gr.ClearButton([chat_input, chatbot, audio_response_checkbox], show_api=False)
64
 
65
+ chat_input.submit(bot, [chat_input, chatbot, audio_response_checkbox], [chat_input, chatbot, audio_response_checkbox], api_name="llm_inference")
66
 
67
+ demo.launch()