muhammadsalmanalfaridzi commited on
Commit
61821e2
·
verified ·
1 Parent(s): 5b34040

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -16
app.py CHANGED
@@ -3,9 +3,9 @@ import os
3
  import warnings
4
  import asyncio
5
  from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Document, Settings
6
- from llama_index.llms.cerebras import Cerebras
7
  from llama_index.embeddings.huggingface import HuggingFaceEmbedding
8
- from groq import Groq
9
  import io
10
 
11
  # Suppress warnings
@@ -24,8 +24,8 @@ else:
24
 
25
  # Initialize Cerebras LLM and embedding model
26
  os.environ["CEREBRAS_API_KEY"] = api_key
27
- llm = Cerebras(model="llama3.1-70b", api_key=os.environ["CEREBRAS_API_KEY"])
28
- Settings.llm = llm
29
  embed_model = HuggingFaceEmbedding(model_name="sentence-transformers/all-MiniLM-L6-v2")
30
 
31
  # Initialize Groq client for Whisper Large V3
@@ -34,7 +34,7 @@ if not groq_api_key:
34
  raise ValueError("GROQ_API_KEY is not set.")
35
  else:
36
  print("Groq API key loaded successfully.")
37
- client = Groq(api_key=groq_api_key)
38
 
39
  # Function for audio transcription and translation (Whisper Large V3 from Groq)
40
  def transcribe_or_translate_audio(audio_file, translate=False):
@@ -46,7 +46,7 @@ def transcribe_or_translate_audio(audio_file, translate=False):
46
  if translate:
47
  result = client.audio.translations.create(
48
  file=(audio_file, file.read()),
49
- model="whisper-large-v3",
50
  response_format="json",
51
  temperature=0.0
52
  )
@@ -54,7 +54,7 @@ def transcribe_or_translate_audio(audio_file, translate=False):
54
  else:
55
  result = client.audio.transcriptions.create(
56
  file=(audio_file, file.read()),
57
- model="whisper-large-v3",
58
  response_format="json",
59
  temperature=0.0
60
  )
@@ -139,20 +139,14 @@ with gr.Blocks() as demo:
139
  msg = gr.Textbox(label="Enter your question")
140
  audio_input = gr.Audio(type="filepath", label="Upload Audio")
141
  translate_checkbox = gr.Checkbox(label="Translate Audio to English Text", value=False)
142
- chatbot = gr.Chatbot(type='messages') # Menggunakan format 'messages'
143
  clear = gr.Button("Clear")
144
 
145
- # Placeholder state untuk input None
146
- state_placeholder = gr.State(value=None)
147
-
148
  # Set up event handlers
149
  load_btn.click(load_documents, inputs=[file_input], outputs=[load_output])
150
 
151
- # Event handler untuk teks
152
- msg.submit(perform_rag, inputs=[msg, chatbot, state_placeholder, translate_checkbox], outputs=[chatbot])
153
-
154
- # Event handler untuk audio
155
- audio_input.change(perform_rag, inputs=[state_placeholder, chatbot, audio_input, translate_checkbox], outputs=[chatbot])
156
 
157
  clear.click(clear_all, outputs=[file_input, load_output, chatbot, msg], queue=False)
158
 
 
3
  import warnings
4
  import asyncio
5
  from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Document, Settings
6
+ from llama_index.llms.cerebras import Cerebras # Import Cerebras LLM
7
  from llama_index.embeddings.huggingface import HuggingFaceEmbedding
8
+ from groq import Groq # Import Groq API for Whisper Large V3
9
  import io
10
 
11
  # Suppress warnings
 
24
 
25
  # Initialize Cerebras LLM and embedding model
26
  os.environ["CEREBRAS_API_KEY"] = api_key
27
+ llm = Cerebras(model="llama3.1-70b", api_key=os.environ["CEREBRAS_API_KEY"]) # Change model to Llama3.1-70b from Cerebras
28
+ Settings.llm = llm # Ensure Cerebras is the LLM being used
29
  embed_model = HuggingFaceEmbedding(model_name="sentence-transformers/all-MiniLM-L6-v2")
30
 
31
  # Initialize Groq client for Whisper Large V3
 
34
  raise ValueError("GROQ_API_KEY is not set.")
35
  else:
36
  print("Groq API key loaded successfully.")
37
+ client = Groq(api_key=groq_api_key) # Groq client initialization
38
 
39
  # Function for audio transcription and translation (Whisper Large V3 from Groq)
40
  def transcribe_or_translate_audio(audio_file, translate=False):
 
46
  if translate:
47
  result = client.audio.translations.create(
48
  file=(audio_file, file.read()),
49
+ model="whisper-large-v3", # Use Groq Whisper Large V3
50
  response_format="json",
51
  temperature=0.0
52
  )
 
54
  else:
55
  result = client.audio.transcriptions.create(
56
  file=(audio_file, file.read()),
57
+ model="whisper-large-v3", # Use Groq Whisper Large V3
58
  response_format="json",
59
  temperature=0.0
60
  )
 
139
  msg = gr.Textbox(label="Enter your question")
140
  audio_input = gr.Audio(type="filepath", label="Upload Audio")
141
  translate_checkbox = gr.Checkbox(label="Translate Audio to English Text", value=False)
142
+ chatbot = gr.Chatbot()
143
  clear = gr.Button("Clear")
144
 
 
 
 
145
  # Set up event handlers
146
  load_btn.click(load_documents, inputs=[file_input], outputs=[load_output])
147
 
148
+ # Event handler for audio input to directly trigger processing and chat response
149
+ audio_input.change(perform_rag, inputs=[msg, chatbot, audio_input, translate_checkbox], outputs=[chatbot])
 
 
 
150
 
151
  clear.click(clear_all, outputs=[file_input, load_output, chatbot, msg], queue=False)
152