Update app.py
Browse files
app.py
CHANGED
|
@@ -1,219 +1,50 @@
|
|
| 1 |
-
# import gradio as gr
|
| 2 |
-
# import groq
|
| 3 |
-
# from config.settings import settings
|
| 4 |
-
# from core.rag_system import EnhancedRAGSystem
|
| 5 |
-
# from core.tts_service import EnhancedTTSService
|
| 6 |
-
# from core.wikipedia_processor import WikipediaProcessor
|
| 7 |
-
# from services.audio_service import AudioService
|
| 8 |
-
# from services.chat_service import ChatService
|
| 9 |
-
# from services.image_service import ImageService
|
| 10 |
-
# from services.streaming_voice_service import StreamingVoiceService
|
| 11 |
-
# from ui.components import create_custom_css, create_header
|
| 12 |
-
# from ui.tabs import create_all_tabs
|
| 13 |
-
# def main():
|
| 14 |
-
# # Initialize clients and services
|
| 15 |
-
# if not settings.GROQ_API_KEY:
|
| 16 |
-
# raise ValueError("Please set the GROQ_API_KEY environment variable.")
|
| 17 |
-
|
| 18 |
-
# client = groq.Client(api_key=settings.GROQ_API_KEY)
|
| 19 |
-
|
| 20 |
-
# # Initialize core systems
|
| 21 |
-
# rag_system = EnhancedRAGSystem()
|
| 22 |
-
# tts_service = EnhancedTTSService()
|
| 23 |
-
# wikipedia_processor = WikipediaProcessor()
|
| 24 |
-
|
| 25 |
-
# # Initialize services
|
| 26 |
-
# audio_service = AudioService(client, rag_system, tts_service)
|
| 27 |
-
# chat_service = ChatService(client, rag_system, tts_service)
|
| 28 |
-
# image_service = ImageService(client)
|
| 29 |
-
# streaming_voice_service = StreamingVoiceService(client, rag_system, tts_service)
|
| 30 |
-
|
| 31 |
-
# # Create Gradio interface
|
| 32 |
-
# with gr.Blocks(css=create_custom_css(), theme=gr.themes.Soft(primary_hue="orange", neutral_hue="slate")) as demo:
|
| 33 |
-
# create_header()
|
| 34 |
-
# gr.Markdown("### 🌐 Hệ thống Đa ngôn ngữ - Tự động chuyển đổi model theo ngôn ngữ")
|
| 35 |
-
# create_all_tabs(
|
| 36 |
-
# audio_service=audio_service,
|
| 37 |
-
# chat_service=chat_service,
|
| 38 |
-
# image_service=image_service,
|
| 39 |
-
# rag_system=rag_system,
|
| 40 |
-
# tts_service=tts_service,
|
| 41 |
-
# wikipedia_processor=wikipedia_processor,
|
| 42 |
-
# streaming_voice_service=streaming_voice_service
|
| 43 |
-
# )
|
| 44 |
-
|
| 45 |
-
# return demo
|
| 46 |
-
|
| 47 |
-
# if __name__ == "__main__":
|
| 48 |
-
# demo = main()
|
| 49 |
-
# demo.launch(share=True)
|
| 50 |
-
|
| 51 |
import gradio as gr
|
| 52 |
-
import os
|
| 53 |
import groq
|
| 54 |
-
import
|
| 55 |
-
from
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
response = client.chat.completions.create(
|
| 95 |
-
model="llama-3.1-8b-instant",
|
| 96 |
-
messages=[
|
| 97 |
-
{"role": "system", "content": "Bạn là trợ lý AI hữu ích. Trả lời ngắn gọn bằng tiếng Việt."},
|
| 98 |
-
{"role": "user", "content": message}
|
| 99 |
-
],
|
| 100 |
-
max_tokens=150,
|
| 101 |
-
temperature=0.7
|
| 102 |
)
|
| 103 |
-
|
| 104 |
-
bot_message = response.choices[0].message.content
|
| 105 |
-
history.append((message, bot_message))
|
| 106 |
-
|
| 107 |
-
except Exception as e:
|
| 108 |
-
error_msg = f"❌ Lỗi: {str(e)}"
|
| 109 |
-
history.append((message, error_msg))
|
| 110 |
|
| 111 |
-
return
|
| 112 |
|
| 113 |
-
def simple_audio_transcribe(audio):
|
| 114 |
-
"""Transcribe audio đơn giản"""
|
| 115 |
-
try:
|
| 116 |
-
api_key = os.getenv("GROQ_API_KEY") or os.getenv("X_GROQ_API_KEY")
|
| 117 |
-
|
| 118 |
-
if not api_key:
|
| 119 |
-
return "❌ Lỗi: Không tìm thấy GROQ_API_KEY", "Vui lòng kiểm tra cài đặt", "vi"
|
| 120 |
-
|
| 121 |
-
if audio is None:
|
| 122 |
-
return "🎤 Vui lòng ghi âm hoặc upload file", "Tôi đang lắng nghe...", "vi"
|
| 123 |
-
|
| 124 |
-
client = groq.Client(api_key=api_key)
|
| 125 |
-
|
| 126 |
-
with open(audio, "rb") as audio_file:
|
| 127 |
-
transcription = client.audio.transcriptions.create(
|
| 128 |
-
model="whisper-large-v3-turbo",
|
| 129 |
-
file=audio_file,
|
| 130 |
-
response_format="text",
|
| 131 |
-
language="vi"
|
| 132 |
-
)
|
| 133 |
-
|
| 134 |
-
# Simple response
|
| 135 |
-
response_client = client.chat.completions.create(
|
| 136 |
-
model="llama-3.1-8b-instant",
|
| 137 |
-
messages=[
|
| 138 |
-
{"role": "system", "content": "Bạn là trợ lý AI. Trả lời ngắn gọn bằng tiếng Việt."},
|
| 139 |
-
{"role": "user", "content": transcription}
|
| 140 |
-
],
|
| 141 |
-
max_tokens=100
|
| 142 |
-
)
|
| 143 |
-
|
| 144 |
-
response = response_client.choices[0].message.content
|
| 145 |
-
|
| 146 |
-
return transcription, response, "vi"
|
| 147 |
-
|
| 148 |
-
except Exception as e:
|
| 149 |
-
error_msg = f"❌ Lỗi: {str(e)}"
|
| 150 |
-
return error_msg, error_msg, "vi"
|
| 151 |
-
|
| 152 |
-
# Tạo interface đơn giản
|
| 153 |
-
with gr.Blocks(theme=gr.themes.Soft(), title="AI Assistant - Hugging Face") as demo:
|
| 154 |
-
gr.Markdown("# 🤖 AI Assistant - Hugging Face Spaces")
|
| 155 |
-
gr.Markdown("Phiên bản đơn giản hoạt động trên Hugging Face")
|
| 156 |
-
|
| 157 |
-
# System Test
|
| 158 |
-
with gr.Row():
|
| 159 |
-
test_btn = gr.Button("🔍 Test Hệ Thống", variant="secondary")
|
| 160 |
-
test_output = gr.Textbox(label="Kết Quả Test", interactive=False)
|
| 161 |
-
|
| 162 |
-
test_btn.click(test_system, outputs=[test_output])
|
| 163 |
-
|
| 164 |
-
# Simple Chat
|
| 165 |
-
with gr.Tab("💬 Chat Đơn Giản"):
|
| 166 |
-
chatbot = gr.Chatbot(height=400, label="Cuộc Trò Chuyện")
|
| 167 |
-
msg = gr.Textbox(placeholder="Nhập tin nhắn của bạn...", label="Tin Nhắn")
|
| 168 |
-
clear_btn = gr.Button("🗑️ Xóa Lịch Sử")
|
| 169 |
-
|
| 170 |
-
def clear_chat():
|
| 171 |
-
return []
|
| 172 |
-
|
| 173 |
-
msg.submit(simple_chat, [msg, chatbot], [msg, chatbot])
|
| 174 |
-
clear_btn.click(clear_chat, outputs=[chatbot])
|
| 175 |
-
|
| 176 |
-
# Simple Audio
|
| 177 |
-
with gr.Tab("🎙️ Ghi Âm Đơn Giản"):
|
| 178 |
-
audio_input = gr.Audio(
|
| 179 |
-
sources=["microphone", "upload"],
|
| 180 |
-
type="filepath",
|
| 181 |
-
label="Ghi Âm hoặc Upload File"
|
| 182 |
-
)
|
| 183 |
-
|
| 184 |
-
with gr.Row():
|
| 185 |
-
transcription = gr.Textbox(label="Bản Ghi Âm", lines=3)
|
| 186 |
-
response = gr.Textbox(label="Phản Hồi AI", lines=3)
|
| 187 |
-
language = gr.Textbox(label="Ngôn Ngữ", value="vi")
|
| 188 |
-
|
| 189 |
-
audio_btn = gr.Button("🎯 Xử Lý Âm Thanh", variant="primary")
|
| 190 |
-
|
| 191 |
-
audio_btn.click(
|
| 192 |
-
simple_audio_transcribe,
|
| 193 |
-
inputs=[audio_input],
|
| 194 |
-
outputs=[transcription, response, language]
|
| 195 |
-
)
|
| 196 |
-
|
| 197 |
-
# Instructions
|
| 198 |
-
with gr.Accordion("📖 Hướng Dẫn Sử Dụng", open=False):
|
| 199 |
-
gr.Markdown("""
|
| 200 |
-
## 🔧 Cài Đặt Trên Hugging Face Spaces
|
| 201 |
-
|
| 202 |
-
1. **Thêm GROQ_API_KEY:**
|
| 203 |
-
- Vào Settings → Repository secrets
|
| 204 |
-
- Thêm secret: `GROQ_API_KEY` với giá trị API key từ Groq
|
| 205 |
-
|
| 206 |
-
2. **API Key Format:**
|
| 207 |
-
- Phải bắt đầu bằng `gsk_`
|
| 208 |
-
- Lấy từ: https://console.groq.com
|
| 209 |
-
|
| 210 |
-
3. **Nếu vẫn lỗi:**
|
| 211 |
-
- Kiểm tra quota Groq API
|
| 212 |
-
- Đảm bảo API key có quyền truy cập
|
| 213 |
-
""")
|
| 214 |
if __name__ == "__main__":
|
| 215 |
-
demo
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
debug=True
|
| 219 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import groq
|
| 3 |
+
from config.settings import settings
|
| 4 |
+
from core.rag_system import EnhancedRAGSystem
|
| 5 |
+
from core.tts_service import EnhancedTTSService
|
| 6 |
+
from core.wikipedia_processor import WikipediaProcessor
|
| 7 |
+
from services.audio_service import AudioService
|
| 8 |
+
from services.chat_service import ChatService
|
| 9 |
+
from services.image_service import ImageService
|
| 10 |
+
from services.streaming_voice_service import StreamingVoiceService
|
| 11 |
+
from ui.components import create_custom_css, create_header
|
| 12 |
+
from ui.tabs import create_all_tabs
|
| 13 |
+
def main():
|
| 14 |
+
# Initialize clients and services
|
| 15 |
+
if not settings.GROQ_API_KEY:
|
| 16 |
+
raise ValueError("Please set the GROQ_API_KEY environment variable.")
|
| 17 |
+
|
| 18 |
+
client = groq.Client(api_key=settings.GROQ_API_KEY)
|
| 19 |
+
|
| 20 |
+
# Initialize core systems
|
| 21 |
+
rag_system = EnhancedRAGSystem()
|
| 22 |
+
tts_service = EnhancedTTSService()
|
| 23 |
+
wikipedia_processor = WikipediaProcessor()
|
| 24 |
+
|
| 25 |
+
# Initialize services
|
| 26 |
+
audio_service = AudioService(client, rag_system, tts_service)
|
| 27 |
+
chat_service = ChatService(client, rag_system, tts_service)
|
| 28 |
+
image_service = ImageService(client)
|
| 29 |
+
streaming_voice_service = StreamingVoiceService(client, rag_system, tts_service)
|
| 30 |
+
|
| 31 |
+
# Create Gradio interface
|
| 32 |
+
with gr.Blocks(css=create_custom_css(), theme=gr.themes.Soft(primary_hue="orange", neutral_hue="slate")) as demo:
|
| 33 |
+
create_header()
|
| 34 |
+
gr.Markdown("### 🌐 Hệ thống Đa ngôn ngữ - Tự động chuyển đổi model theo ngôn ngữ")
|
| 35 |
+
create_all_tabs(
|
| 36 |
+
audio_service=audio_service,
|
| 37 |
+
chat_service=chat_service,
|
| 38 |
+
image_service=image_service,
|
| 39 |
+
rag_system=rag_system,
|
| 40 |
+
tts_service=tts_service,
|
| 41 |
+
wikipedia_processor=wikipedia_processor,
|
| 42 |
+
streaming_voice_service=streaming_voice_service
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
return demo
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
if __name__ == "__main__":
|
| 48 |
+
demo = main()
|
| 49 |
+
demo.launch(share=True)
|
| 50 |
+
|
|
|
|
|
|