Spaces:
Runtime error
Runtime error
Delete delete.py
Browse files
delete.py
DELETED
@@ -1,174 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import os
|
3 |
-
import warnings
|
4 |
-
import asyncio
|
5 |
-
from melo.api import TTS
|
6 |
-
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Document, Settings
|
7 |
-
from llama_index.llms.cerebras import Cerebras
|
8 |
-
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
|
9 |
-
from groq import Groq
|
10 |
-
import io
|
11 |
-
import nltk
|
12 |
-
|
13 |
-
nltk.download('averaged_perceptron_tagger_eng')
|
14 |
-
|
15 |
-
# Suppress warnings
|
16 |
-
warnings.filterwarnings("ignore", message=".*clean_up_tokenization_spaces.*")
|
17 |
-
|
18 |
-
# Global variables
|
19 |
-
index = None
|
20 |
-
query_engine = None
|
21 |
-
|
22 |
-
# Inisialisasi MeloTTS untuk TTS
|
23 |
-
device = 'cpu' # Atur menjadi 'cuda' jika GPU tersedia
|
24 |
-
language = 'EN' # Bahasa default
|
25 |
-
model = TTS(language=language, device=device)
|
26 |
-
|
27 |
-
# Load Cerebras API key from Hugging Face secrets
|
28 |
-
api_key = os.getenv("CEREBRAS_API_KEY")
|
29 |
-
if not api_key:
|
30 |
-
raise ValueError("CEREBRAS_API_KEY is not set in Hugging Face Secrets.")
|
31 |
-
else:
|
32 |
-
print("Cerebras API key loaded successfully.")
|
33 |
-
|
34 |
-
# Initialize Cerebras LLM and embedding model
|
35 |
-
os.environ["CEREBRAS_API_KEY"] = api_key
|
36 |
-
llm = Cerebras(model="llama-3.3-70b", api_key=os.environ["CEREBRAS_API_KEY"]) # Change model to Llama3.1-70b from Cerebras
|
37 |
-
Settings.llm = llm # Ensure Cerebras is the LLM being used
|
38 |
-
embed_model = HuggingFaceEmbedding(model_name="sentence-transformers/all-mpnet-base-v2")
|
39 |
-
|
40 |
-
# Initialize Groq client for Whisper Large V3
|
41 |
-
groq_api_key = os.getenv("GROQ_API_KEY")
|
42 |
-
if not groq_api_key:
|
43 |
-
raise ValueError("GROQ_API_KEY is not set.")
|
44 |
-
else:
|
45 |
-
print("Groq API key loaded successfully.")
|
46 |
-
client = Groq(api_key=groq_api_key) # Groq client initialization
|
47 |
-
|
48 |
-
# Function for audio transcription and translation (Whisper Large V3 from Groq)
|
49 |
-
def transcribe_or_translate_audio(audio_file, translate=False):
|
50 |
-
"""
|
51 |
-
Transcribes or translates audio using Whisper Large V3 via Groq API.
|
52 |
-
"""
|
53 |
-
try:
|
54 |
-
with open(audio_file, "rb") as file:
|
55 |
-
if translate:
|
56 |
-
result = client.audio.translations.create(
|
57 |
-
file=(audio_file, file.read()),
|
58 |
-
model="whisper-large-v3", # Use Groq Whisper Large V3
|
59 |
-
response_format="json",
|
60 |
-
temperature=0.0
|
61 |
-
)
|
62 |
-
return result.text
|
63 |
-
else:
|
64 |
-
result = client.audio.transcriptions.create(
|
65 |
-
file=(audio_file, file.read()),
|
66 |
-
model="whisper-large-v3", # Use Groq Whisper Large V3
|
67 |
-
response_format="json",
|
68 |
-
temperature=0.0
|
69 |
-
)
|
70 |
-
return result.text
|
71 |
-
except Exception as e:
|
72 |
-
return f"Error processing audio: {str(e)}"
|
73 |
-
|
74 |
-
# Function to load documents and create index
|
75 |
-
def load_documents(file_objs):
|
76 |
-
global index, query_engine
|
77 |
-
try:
|
78 |
-
if not file_objs:
|
79 |
-
return "Error: No files selected."
|
80 |
-
|
81 |
-
documents = []
|
82 |
-
document_names = []
|
83 |
-
for file_obj in file_objs:
|
84 |
-
file_name = os.path.basename(file_obj.name)
|
85 |
-
document_names.append(file_name)
|
86 |
-
loaded_docs = SimpleDirectoryReader(input_files=[file_obj.name]).load_data()
|
87 |
-
for doc in loaded_docs:
|
88 |
-
doc.metadata["source"] = file_name
|
89 |
-
documents.append(doc)
|
90 |
-
|
91 |
-
if not documents:
|
92 |
-
return "No documents found in the selected files."
|
93 |
-
|
94 |
-
index = VectorStoreIndex.from_documents(documents, llm=llm, embed_model=embed_model)
|
95 |
-
query_engine = index.as_query_engine()
|
96 |
-
|
97 |
-
return f"Successfully loaded {len(documents)} documents from the files: {', '.join(document_names)}"
|
98 |
-
except Exception as e:
|
99 |
-
return f"Error loading documents: {str(e)}"
|
100 |
-
|
101 |
-
async def perform_rag(query, history, audio_file=None, translate_audio=False):
|
102 |
-
global query_engine
|
103 |
-
if query_engine is None:
|
104 |
-
return history + [("Please load documents first.", None)], None # Tambahkan None untuk output audio
|
105 |
-
|
106 |
-
try:
|
107 |
-
# Handle audio input jika diberikan
|
108 |
-
if audio_file:
|
109 |
-
transcription = transcribe_or_translate_audio(audio_file, translate=translate_audio)
|
110 |
-
query = f"{query} {transcription}".strip()
|
111 |
-
|
112 |
-
response = await asyncio.to_thread(query_engine.query, query)
|
113 |
-
answer = str(response) # Dapatkan jawaban dari respons
|
114 |
-
|
115 |
-
# Jika dokumen relevan tersedia, tambahkan sumber tanpa label "Sources"
|
116 |
-
if hasattr(response, "get_documents"):
|
117 |
-
relevant_docs = response.get_documents()
|
118 |
-
if relevant_docs:
|
119 |
-
sources = "\n\n".join([f"{doc.metadata.get('source', 'No source available')}" for doc in relevant_docs])
|
120 |
-
else:
|
121 |
-
sources = ""
|
122 |
-
else:
|
123 |
-
sources = ""
|
124 |
-
|
125 |
-
# Gabungkan jawaban dengan sumber (jika ada) tanpa label tambahan
|
126 |
-
final_result = f"{answer}\n\n{sources}".strip()
|
127 |
-
|
128 |
-
# **Generate audio menggunakan MeloTTS**
|
129 |
-
output_audio_path = "output.wav"
|
130 |
-
model.tts_to_file(answer, model.hps.data.spk2id['EN-US'], output_audio_path, speed=1.0)
|
131 |
-
|
132 |
-
# Kembalikan history yang diperbarui dan file audio
|
133 |
-
return history + [(query, final_result)], output_audio_path
|
134 |
-
except Exception as e:
|
135 |
-
return history + [(query, f"Error processing query: {str(e)}")], None
|
136 |
-
|
137 |
-
# Function to clear the session and reset variables
|
138 |
-
def clear_all():
|
139 |
-
global index, query_engine
|
140 |
-
index = None
|
141 |
-
query_engine = None
|
142 |
-
return None, "", [], "" # Reset file input, load output, chatbot, and message input to default states
|
143 |
-
|
144 |
-
# Create the Gradio interface
|
145 |
-
with gr.Blocks(theme=gr.themes.Base(primary_hue="teal", secondary_hue="teal", neutral_hue="slate")) as demo:
|
146 |
-
gr.Markdown("# RAG Multi-file Chat Application with Speech-to-Text and Text-to-Speech")
|
147 |
-
|
148 |
-
with gr.Row():
|
149 |
-
file_input = gr.File(label="Select files to load", file_count="multiple")
|
150 |
-
load_btn = gr.Button("Load Documents")
|
151 |
-
load_output = gr.Textbox(label="Load Status")
|
152 |
-
|
153 |
-
msg = gr.Textbox(label="Enter your question")
|
154 |
-
audio_input = gr.Audio(type="filepath", label="Upload Audio")
|
155 |
-
translate_checkbox = gr.Checkbox(label="Translate Audio to English Text", value=False)
|
156 |
-
chatbot = gr.Chatbot()
|
157 |
-
audio_output = gr.Audio(label="Response Audio", type="filepath") # Tambahkan output audio
|
158 |
-
clear = gr.Button("Clear")
|
159 |
-
|
160 |
-
# Set up event handlers
|
161 |
-
load_btn.click(load_documents, inputs=[file_input], outputs=[load_output])
|
162 |
-
|
163 |
-
# Event handler untuk input teks (proses teks)
|
164 |
-
msg.submit(perform_rag, inputs=[msg, chatbot], outputs=[chatbot, audio_output]) # Tambahkan audio_output
|
165 |
-
|
166 |
-
# Event handler untuk input audio (proses audio)
|
167 |
-
audio_input.change(perform_rag, inputs=[msg, chatbot, audio_input, translate_checkbox], outputs=[chatbot, audio_output]) # Tambahkan audio_output
|
168 |
-
|
169 |
-
clear.click(clear_all, outputs=[file_input, load_output, chatbot, msg], queue=False)
|
170 |
-
|
171 |
-
# Run the app
|
172 |
-
if __name__ == "__main__":
|
173 |
-
demo.queue()
|
174 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|