freddyaboulton HF staff commited on
Commit
d52ab93
1 Parent(s): 6660df8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -43
app.py CHANGED
@@ -1,43 +1,45 @@
1
  import gradio as gr
2
- from gradio_webrtc import WebRTC, ReplyOnPause, AdditionalOutputs
3
  import numpy as np
4
  import os
5
- from twilio.rest import Client
6
  import base64
7
  import openai
8
  import re
9
- from huggingface_hub import InferenceClient
10
- from pydub import AudioSegment
11
- import io
12
 
13
  from dotenv import load_dotenv
14
  load_dotenv()
15
 
16
- hf_client = InferenceClient()
17
-
18
  spinner_html = open("spinner.html").read()
19
 
 
 
 
20
 
21
- account_sid = os.environ.get("TWILIO_ACCOUNT_SID")
22
- auth_token = os.environ.get("TWILIO_AUTH_TOKEN")
 
23
 
24
- if account_sid and auth_token:
25
- client = Client(account_sid, auth_token)
 
26
 
27
- token = client.tokens.create()
 
 
28
 
29
- rtc_configuration = {
30
- "iceServers": token.ice_servers,
31
- "iceTransportPolicy": "relay",
32
- }
33
- else:
34
- rtc_configuration = None
35
 
36
  client = openai.OpenAI(
37
  api_key=os.environ.get("SAMBANOVA_API_KEY"),
38
  base_url="https://api.sambanova.ai/v1",
39
  )
40
 
 
 
41
 
42
  system_prompt = "You are an AI coding assistant. Your task is to write single-file HTML applications based on a user's request. Only return the necessary code. Include all necessary imports and styles. You may also be asked to edit your original response."
43
  user_prompt = "Please write a single-file HTML application to fulfill the following request.\nThe message:{user_message}\nCurrent code you have written:{code}"
@@ -50,24 +52,6 @@ def extract_html_content(text):
50
  return match.group(0) if match else None
51
 
52
 
53
- def audio_to_bytes(audio: tuple[int, np.ndarray]):
54
- audio_segment = AudioSegment(
55
- audio[1].squeeze().tobytes(),
56
- frame_rate=audio[0],
57
- sample_width=audio[1].dtype.itemsize,
58
- channels=1
59
- )
60
-
61
- # Export the audio segment to MP3 bytes - use a high bitrate to maximise quality
62
- mp3_io = io.BytesIO()
63
- audio_segment.export(mp3_io, format="mp3", bitrate="320k")
64
-
65
- # Get the MP3 bytes
66
- mp3_bytes = mp3_io.getvalue()
67
- mp3_io.close()
68
- return mp3_bytes
69
-
70
-
71
  def display_in_sandbox(code):
72
  encoded_html = base64.b64encode(code.encode('utf-8')).decode('utf-8')
73
  data_uri = f"data:text/html;charset=utf-8;base64,{encoded_html}"
@@ -79,7 +63,14 @@ def generate(user_message: tuple[int, np.ndarray],
79
  code: str):
80
  yield AdditionalOutputs(history, spinner_html)
81
 
82
- text = hf_client.automatic_speech_recognition(audio_to_bytes(user_message)).text
 
 
 
 
 
 
 
83
 
84
  user_msg_formatted = user_prompt.format(user_message=text, code=code)
85
  history.append({"role": "user", "content": user_msg_formatted})
@@ -104,13 +95,10 @@ with gr.Blocks(css=".code-component {max-height: 500px !important}") as demo:
104
  gr.HTML(
105
  """
106
  <h1 style='text-align: center'>
107
- Llama Code Editor
108
  </h1>
109
- <h2 style='text-align: center'>
110
- Powered by SambaNova and Gradio-WebRTC ⚡️
111
- </h2>
112
  <p style='text-align: center'>
113
- Create and edit single-file HTML applications with just your voice!
114
  </p>
115
  <p style='text-align: center'>
116
  Each conversation is limited to 90 seconds. Once the time limit is up you can rejoin the conversation.
@@ -128,7 +116,9 @@ with gr.Blocks(css=".code-component {max-height: 500px !important}") as demo:
128
  with gr.Tab("Chat"):
129
  cb = gr.Chatbot(type="messages")
130
 
131
- webrtc.stream(ReplyOnPause(generate),
 
 
132
  inputs=[webrtc, history, code],
133
  outputs=[webrtc], time_limit=90,
134
  concurrency_limit=10)
 
1
  import gradio as gr
2
+ from gradio_webrtc import WebRTC, ReplyOnStopWords, AdditionalOutputs, audio_to_bytes
3
  import numpy as np
4
  import os
 
5
  import base64
6
  import openai
7
  import re
8
+ from groq import Groq
 
 
9
 
10
  from dotenv import load_dotenv
11
  load_dotenv()
12
 
 
 
13
  spinner_html = open("spinner.html").read()
14
 
15
+ rtc_configuration = None
16
+
17
+ print("rtc_configuration", rtc_configuration)
18
 
19
+ import logging
20
+ # Configure the root logger to WARNING to suppress debug messages from other libraries
21
+ logging.basicConfig(level=logging.WARNING)
22
 
23
+ # Create a console handler
24
+ console_handler = logging.FileHandler("gradio_webrtc.log")
25
+ console_handler.setLevel(logging.DEBUG)
26
 
27
+ # Create a formatter
28
+ formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
29
+ console_handler.setFormatter(formatter)
30
 
31
+ # Configure the logger for your specific library
32
+ logger = logging.getLogger("gradio_webrtc")
33
+ logger.setLevel(logging.DEBUG)
34
+ logger.addHandler(console_handler)
 
 
35
 
36
  client = openai.OpenAI(
37
  api_key=os.environ.get("SAMBANOVA_API_KEY"),
38
  base_url="https://api.sambanova.ai/v1",
39
  )
40
 
41
+ groq_client = Groq()
42
+
43
 
44
  system_prompt = "You are an AI coding assistant. Your task is to write single-file HTML applications based on a user's request. Only return the necessary code. Include all necessary imports and styles. You may also be asked to edit your original response."
45
  user_prompt = "Please write a single-file HTML application to fulfill the following request.\nThe message:{user_message}\nCurrent code you have written:{code}"
 
52
  return match.group(0) if match else None
53
 
54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
  def display_in_sandbox(code):
56
  encoded_html = base64.b64encode(code.encode('utf-8')).decode('utf-8')
57
  data_uri = f"data:text/html;charset=utf-8;base64,{encoded_html}"
 
63
  code: str):
64
  yield AdditionalOutputs(history, spinner_html)
65
 
66
+ sr, audio = user_message
67
+ audio = audio.squeeze()
68
+
69
+ text = groq_client.audio.transcriptions.create(
70
+ file=("audio-file.mp3", audio_to_bytes((sr, audio))),
71
+ model="whisper-large-v3-turbo",
72
+ response_format="verbose_json",
73
+ ).text
74
 
75
  user_msg_formatted = user_prompt.format(user_message=text, code=code)
76
  history.append({"role": "user", "content": user_msg_formatted})
 
95
  gr.HTML(
96
  """
97
  <h1 style='text-align: center'>
98
+ Hello Llama! 🦙
99
  </h1>
 
 
 
100
  <p style='text-align: center'>
101
+ Create and edit single-file HTML applications with just your voice! After recording, say "Hey Llama" and wait for confirmation, before asking your question.
102
  </p>
103
  <p style='text-align: center'>
104
  Each conversation is limited to 90 seconds. Once the time limit is up you can rejoin the conversation.
 
116
  with gr.Tab("Chat"):
117
  cb = gr.Chatbot(type="messages")
118
 
119
+ webrtc.stream(ReplyOnStopWords(generate,
120
+ input_sample_rate=16000,
121
+ stop_words=["hello llama", "hello lama", "hello lamma", "hello llamma"]),
122
  inputs=[webrtc, history, code],
123
  outputs=[webrtc], time_limit=90,
124
  concurrency_limit=10)