Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,10 +4,15 @@ os.environ["CUDA_VISIBLE_DEVICES"] = ""
|
|
| 4 |
import gradio as gr
|
| 5 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 6 |
import spaces
|
|
|
|
|
|
|
|
|
|
| 7 |
|
|
|
|
|
|
|
|
|
|
| 8 |
@spaces.GPU(duration=60)
|
| 9 |
def chat_response(message, history):
|
| 10 |
-
# β
ALL YOUR RULES β BUILT-IN "TRAINING"!
|
| 11 |
system_prompt = """You are a helpful assistant supporting NDIS care work and documentation.
|
| 12 |
|
| 13 |
YOUR RULES β FOLLOW ALWAYS:
|
|
@@ -22,15 +27,12 @@ YOUR RULES β FOLLOW ALWAYS:
|
|
| 22 |
9. REMEMBER what we discussed earlier β treat THIS conversation as ONE continuous document.
|
| 23 |
"""
|
| 24 |
|
| 25 |
-
# β
BUILD FULL CONVERSATION β AI REMEMBERS EVERYTHING!
|
| 26 |
full_conversation = f"<|im_start|>system\n{system_prompt}<|im_end|>\n"
|
| 27 |
-
|
| 28 |
-
# Feed ALL previous messages
|
| 29 |
for user_msg, assistant_msg in history:
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
full_conversation += f"<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n"
|
| 35 |
|
| 36 |
output = chat_pipeline(
|
|
@@ -45,7 +47,20 @@ YOUR RULES β FOLLOW ALWAYS:
|
|
| 45 |
reply = output.split("<|im_start|>assistant\n")[-1].split("<|im_end|>")[0].strip()
|
| 46 |
return reply
|
| 47 |
|
| 48 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
model_id = "Qwen/Qwen2.5-1.5B-Instruct"
|
| 50 |
|
| 51 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
|
@@ -62,12 +77,79 @@ chat_pipeline = pipeline(
|
|
| 62 |
pad_token_id=tokenizer.eos_token_id
|
| 63 |
)
|
| 64 |
|
| 65 |
-
#
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
if __name__ == "__main__":
|
| 73 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 4 |
import gradio as gr
|
| 5 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 6 |
import spaces
|
| 7 |
+
from gtts import gTTS
|
| 8 |
+
import io
|
| 9 |
+
import base64
|
| 10 |
|
| 11 |
+
# ----------------------
|
| 12 |
+
# AI Response Engine
|
| 13 |
+
# ----------------------
|
| 14 |
@spaces.GPU(duration=60)
|
| 15 |
def chat_response(message, history):
|
|
|
|
| 16 |
system_prompt = """You are a helpful assistant supporting NDIS care work and documentation.
|
| 17 |
|
| 18 |
YOUR RULES β FOLLOW ALWAYS:
|
|
|
|
| 27 |
9. REMEMBER what we discussed earlier β treat THIS conversation as ONE continuous document.
|
| 28 |
"""
|
| 29 |
|
|
|
|
| 30 |
full_conversation = f"<|im_start|>system\n{system_prompt}<|im_end|>\n"
|
|
|
|
|
|
|
| 31 |
for user_msg, assistant_msg in history:
|
| 32 |
+
if user_msg:
|
| 33 |
+
full_conversation += f"<|im_start|>user\n{user_msg}<|im_end|>\n"
|
| 34 |
+
if assistant_msg:
|
| 35 |
+
full_conversation += f"<|im_start|>assistant\n{assistant_msg}<|im_end|>\n"
|
| 36 |
full_conversation += f"<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n"
|
| 37 |
|
| 38 |
output = chat_pipeline(
|
|
|
|
| 47 |
reply = output.split("<|im_start|>assistant\n")[-1].split("<|im_end|>")[0].strip()
|
| 48 |
return reply
|
| 49 |
|
| 50 |
+
# ----------------------
|
| 51 |
+
# Realistic Voice Generator
|
| 52 |
+
# ----------------------
|
| 53 |
+
def text_to_realistic_voice(text):
|
| 54 |
+
"""Convert AI reply to natural-sounding Australian English voice"""
|
| 55 |
+
tts = gTTS(text=text, lang='en-au', slow=False) # en-au = Australian clear natural voice
|
| 56 |
+
audio_fp = io.BytesIO()
|
| 57 |
+
tts.write_to_fp(audio_fp)
|
| 58 |
+
audio_fp.seek(0)
|
| 59 |
+
return audio_fp
|
| 60 |
+
|
| 61 |
+
# ----------------------
|
| 62 |
+
# Load Qwen AI Model
|
| 63 |
+
# ----------------------
|
| 64 |
model_id = "Qwen/Qwen2.5-1.5B-Instruct"
|
| 65 |
|
| 66 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
|
|
|
| 77 |
pad_token_id=tokenizer.eos_token_id
|
| 78 |
)
|
| 79 |
|
| 80 |
+
# ----------------------
|
| 81 |
+
# Combined Handler
|
| 82 |
+
# ----------------------
|
| 83 |
+
def process_input(message, history, audio_input=None):
|
| 84 |
+
# Handle voice-to-text first if spoken
|
| 85 |
+
transcribed = ""
|
| 86 |
+
if audio_input is not None:
|
| 87 |
+
transcribed = gr.Audio.transcribe(audio_input)
|
| 88 |
+
if not transcribed.strip():
|
| 89 |
+
return "", history, None, None
|
| 90 |
+
message = transcribed
|
| 91 |
+
|
| 92 |
+
if not message.strip():
|
| 93 |
+
return "", history, None, None
|
| 94 |
+
|
| 95 |
+
# Get AI reply
|
| 96 |
+
reply = chat_response(message, history)
|
| 97 |
+
history = history + [(message, reply)]
|
| 98 |
+
|
| 99 |
+
# Generate realistic voice output
|
| 100 |
+
voice_audio = text_to_realistic_voice(reply)
|
| 101 |
+
|
| 102 |
+
return "", history, voice_audio, transcribed
|
| 103 |
+
|
| 104 |
+
# ----------------------
|
| 105 |
+
# Build Interface
|
| 106 |
+
# ----------------------
|
| 107 |
+
with gr.Blocks(title="ForgeSync β Realistic Voice AI") as demo:
|
| 108 |
+
gr.Markdown("""
|
| 109 |
+
# π€ ForgeSync β Voice & Text AI
|
| 110 |
+
π€ **Speak** or **type** β I'll respond in **clear natural voice** + written text.
|
| 111 |
+
π Everything is transcribed and saved.
|
| 112 |
+
β οΈ Always review before saving!
|
| 113 |
+
""")
|
| 114 |
+
|
| 115 |
+
# Chat display
|
| 116 |
+
chatbot = gr.Chatbot(label="π Conversation & Scribed Notes", height=450)
|
| 117 |
+
|
| 118 |
+
with gr.Row():
|
| 119 |
+
txt_input = gr.Textbox(
|
| 120 |
+
label="Type your note or question",
|
| 121 |
+
placeholder="Or use the microphone below...",
|
| 122 |
+
scale=3
|
| 123 |
+
)
|
| 124 |
+
audio_in = gr.Audio(
|
| 125 |
+
label="π€ Speak",
|
| 126 |
+
sources=["microphone"],
|
| 127 |
+
type="filepath"
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
transcribed_display = gr.Textbox(
|
| 131 |
+
label="π What I heard β edit if needed",
|
| 132 |
+
interactive=True
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
audio_out = gr.Audio(
|
| 136 |
+
label="π AI Reply β Natural Voice",
|
| 137 |
+
autoplay=True
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
# Text submission
|
| 141 |
+
txt_input.submit(
|
| 142 |
+
process_input,
|
| 143 |
+
inputs=[txt_input, chatbot, gr.State(None)],
|
| 144 |
+
outputs=[txt_input, chatbot, audio_out, transcribed_display]
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
# Voice submission
|
| 148 |
+
audio_in.change(
|
| 149 |
+
process_input,
|
| 150 |
+
inputs=[gr.State(""), chatbot, audio_in],
|
| 151 |
+
outputs=[txt_input, chatbot, audio_out, transcribed_display]
|
| 152 |
+
)
|
| 153 |
|
| 154 |
if __name__ == "__main__":
|
| 155 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|