Update app.py
Browse files
app.py
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
import gradio as gr
|
| 3 |
from transformers import AutoModel, AutoTokenizer
|
| 4 |
-
import warnings
|
| 5 |
-
warnings.filterwarnings("ignore")
|
| 6 |
|
| 7 |
MODEL_NAME = "openbmb/MiniCPM-o-2_6"
|
| 8 |
|
|
@@ -13,7 +14,7 @@ model = AutoModel.from_pretrained(
|
|
| 13 |
trust_remote_code=True,
|
| 14 |
torch_dtype=torch.float32,
|
| 15 |
low_cpu_mem_usage=True,
|
| 16 |
-
attn_implementation="eager"
|
| 17 |
)
|
| 18 |
|
| 19 |
model = model.eval()
|
|
@@ -23,48 +24,47 @@ tokenizer = AutoTokenizer.from_pretrained(
|
|
| 23 |
trust_remote_code=True
|
| 24 |
)
|
| 25 |
|
| 26 |
-
# TTS
|
| 27 |
try:
|
| 28 |
model.init_tts()
|
| 29 |
print("TTS ready")
|
| 30 |
except Exception as e:
|
| 31 |
-
print("TTS
|
| 32 |
|
| 33 |
-
def
|
| 34 |
|
| 35 |
msgs = [
|
| 36 |
{"role": "user", "content": [text]}
|
| 37 |
]
|
| 38 |
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
|
| 42 |
-
msgs=msgs,
|
| 43 |
-
tokenizer=tokenizer,
|
| 44 |
-
generate_audio=True,
|
| 45 |
-
output_audio_path=output_path,
|
| 46 |
-
temperature=0.3,
|
| 47 |
-
max_new_tokens=200
|
| 48 |
-
)
|
| 49 |
|
| 50 |
-
|
| 51 |
-
reply_text = result if isinstance(result, str) else "Response generated"
|
| 52 |
|
| 53 |
-
|
|
|
|
| 54 |
|
| 55 |
|
| 56 |
-
# UI
|
| 57 |
demo = gr.Interface(
|
| 58 |
-
fn=
|
| 59 |
-
inputs=gr.Textbox(label="
|
| 60 |
outputs=[
|
| 61 |
-
gr.Textbox(label="
|
| 62 |
-
gr.Audio(label="Voice Output
|
| 63 |
],
|
| 64 |
-
title="MiniCPM Voice
|
| 65 |
)
|
| 66 |
|
| 67 |
-
demo.launch(
|
| 68 |
-
server_name="0.0.0.0",
|
| 69 |
-
server_port=7860
|
| 70 |
-
)
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
os.environ["TRANSFORMERS_NO_FLASH_ATTN"] = "1"
|
| 3 |
+
|
| 4 |
import torch
|
| 5 |
import gradio as gr
|
| 6 |
from transformers import AutoModel, AutoTokenizer
|
|
|
|
|
|
|
| 7 |
|
| 8 |
MODEL_NAME = "openbmb/MiniCPM-o-2_6"
|
| 9 |
|
|
|
|
| 14 |
trust_remote_code=True,
|
| 15 |
torch_dtype=torch.float32,
|
| 16 |
low_cpu_mem_usage=True,
|
| 17 |
+
attn_implementation="eager" # 🔥 IMPORTANT FIX
|
| 18 |
)
|
| 19 |
|
| 20 |
model = model.eval()
|
|
|
|
| 24 |
trust_remote_code=True
|
| 25 |
)
|
| 26 |
|
| 27 |
+
# Init TTS safely
|
| 28 |
try:
|
| 29 |
model.init_tts()
|
| 30 |
print("TTS ready")
|
| 31 |
except Exception as e:
|
| 32 |
+
print("TTS not available:", e)
|
| 33 |
|
| 34 |
+
def chat(text):
|
| 35 |
|
| 36 |
msgs = [
|
| 37 |
{"role": "user", "content": [text]}
|
| 38 |
]
|
| 39 |
|
| 40 |
+
audio_path = "output.wav"
|
| 41 |
+
|
| 42 |
+
try:
|
| 43 |
+
result = model.chat(
|
| 44 |
+
msgs=msgs,
|
| 45 |
+
tokenizer=tokenizer,
|
| 46 |
+
generate_audio=True,
|
| 47 |
+
output_audio_path=audio_path,
|
| 48 |
+
max_new_tokens=200,
|
| 49 |
+
temperature=0.3
|
| 50 |
+
)
|
| 51 |
|
| 52 |
+
reply = result if isinstance(result, str) else text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
return reply, audio_path
|
|
|
|
| 55 |
|
| 56 |
+
except Exception as e:
|
| 57 |
+
return str(e), None
|
| 58 |
|
| 59 |
|
|
|
|
| 60 |
demo = gr.Interface(
|
| 61 |
+
fn=chat,
|
| 62 |
+
inputs=gr.Textbox(label="Type message"),
|
| 63 |
outputs=[
|
| 64 |
+
gr.Textbox(label="Reply"),
|
| 65 |
+
gr.Audio(label="Voice Output")
|
| 66 |
],
|
| 67 |
+
title="MiniCPM-o Voice Chatbot (CPU Stable)"
|
| 68 |
)
|
| 69 |
|
| 70 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
|
|
|
|
|