Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,62 +1,70 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
# βββ Load
|
| 5 |
chat_pipe = pipeline(
|
| 6 |
"text-generation",
|
| 7 |
model="Hulk810154/Kai",
|
| 8 |
trust_remote_code=True
|
| 9 |
-
)
|
|
|
|
| 10 |
asr_pipe = pipeline(
|
| 11 |
"automatic-speech-recognition",
|
| 12 |
model="openai/whisper-tiny"
|
| 13 |
-
)
|
| 14 |
|
| 15 |
-
# βββ
|
| 16 |
def chat_fn(message, history):
|
| 17 |
-
|
| 18 |
-
for u, b in history
|
| 19 |
-
prompt += f"User: {u}\nAI: {b}\n"
|
| 20 |
prompt += f"User: {message}\nAI:"
|
| 21 |
out = chat_pipe(prompt, max_length=256, do_sample=True)
|
| 22 |
reply = out[0]["generated_text"][len(prompt):].strip()
|
| 23 |
history.append((message, reply))
|
| 24 |
return history, history
|
| 25 |
|
| 26 |
-
def
|
| 27 |
-
|
|
|
|
| 28 |
return ""
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
def handle_upload(files):
|
| 32 |
-
|
|
|
|
| 33 |
|
| 34 |
-
# βββ UI βββ
|
| 35 |
-
with gr.Blocks() as demo:
|
| 36 |
-
gr.Markdown("# π§ Kai AGI Chat\
|
| 37 |
-
state = gr.State([])
|
| 38 |
|
| 39 |
with gr.Row():
|
| 40 |
with gr.Column():
|
| 41 |
-
chatbot = gr.Chatbot(label="Conversation")
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
with gr.Column():
|
| 46 |
uploader = gr.File(file_count="multiple", label="π Upload Files/Images")
|
| 47 |
upload_status = gr.Textbox(label="Upload Status")
|
| 48 |
|
| 49 |
-
#
|
| 50 |
-
|
| 51 |
-
lambda txt, mic,
|
| 52 |
-
inputs=[
|
| 53 |
-
outputs=[
|
| 54 |
).then(
|
| 55 |
chat_fn,
|
| 56 |
-
inputs=[
|
| 57 |
outputs=[chatbot, state]
|
| 58 |
)
|
| 59 |
|
|
|
|
| 60 |
uploader.upload(handle_upload, inputs=uploader, outputs=upload_status)
|
| 61 |
|
| 62 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# βββ Load models βββ
|
| 5 |
chat_pipe = pipeline(
|
| 6 |
"text-generation",
|
| 7 |
model="Hulk810154/Kai",
|
| 8 |
trust_remote_code=True
|
| 9 |
+
) # Text generation ξ¨6ξ¨
|
| 10 |
+
|
| 11 |
asr_pipe = pipeline(
|
| 12 |
"automatic-speech-recognition",
|
| 13 |
model="openai/whisper-tiny"
|
| 14 |
+
) # Whisper Tiny for STT ξ¨7ξ¨
|
| 15 |
|
| 16 |
+
# βββ Core handlers βββ
|
| 17 |
def chat_fn(message, history):
|
| 18 |
+
"""Generate reply given message + history."""
|
| 19 |
+
prompt = "".join(f"User: {u}\nAI: {b}\n" for u, b in history)
|
|
|
|
| 20 |
prompt += f"User: {message}\nAI:"
|
| 21 |
out = chat_pipe(prompt, max_length=256, do_sample=True)
|
| 22 |
reply = out[0]["generated_text"][len(prompt):].strip()
|
| 23 |
history.append((message, reply))
|
| 24 |
return history, history
|
| 25 |
|
| 26 |
+
def transcribe(audio):
|
| 27 |
+
"""Turn recorded audio into text."""
|
| 28 |
+
if audio is None:
|
| 29 |
return ""
|
| 30 |
+
# gr.Audio returns (array, sampling_rate) by default ξ¨8ξ¨
|
| 31 |
+
if isinstance(audio, (tuple, list)) and len(audio) == 2:
|
| 32 |
+
arr, sr = audio
|
| 33 |
+
return asr_pipe({"array": arr, "sampling_rate": sr})["text"]
|
| 34 |
+
# Or accept a filepath
|
| 35 |
+
return asr_pipe(audio)["text"]
|
| 36 |
|
| 37 |
def handle_upload(files):
|
| 38 |
+
"""Report uploaded files/images."""
|
| 39 |
+
return f"Received {len(files)} file(s): " + ", ".join(f.name for f in files)
|
| 40 |
|
| 41 |
+
# βββ Build UI βββ
|
| 42 |
+
with gr.Blocks() as demo: # Low-level Blocks API ξ¨9ξ¨
|
| 43 |
+
gr.Markdown("# π§ Kai AGI Chat\n_Chat via text, voice, or upload β true multimodal AGI_")
|
| 44 |
+
state = gr.State([]) # Session memory
|
| 45 |
|
| 46 |
with gr.Row():
|
| 47 |
with gr.Column():
|
| 48 |
+
chatbot = gr.Chatbot(type="messages", label="Conversation") # Future-proof format ξ¨10ξ¨
|
| 49 |
+
txt_input = gr.Textbox(placeholder="Type hereβ¦", label="Text Input")
|
| 50 |
+
mic_input = gr.Audio(label="π€ Voice Input") # Browser mic/upload ξ¨11ξ¨
|
| 51 |
+
send_btn = gr.Button("Send")
|
| 52 |
with gr.Column():
|
| 53 |
uploader = gr.File(file_count="multiple", label="π Upload Files/Images")
|
| 54 |
upload_status = gr.Textbox(label="Upload Status")
|
| 55 |
|
| 56 |
+
# Text or voice β chat
|
| 57 |
+
send_btn.click(
|
| 58 |
+
lambda txt, mic, hist: (transcribe(mic) or txt, hist),
|
| 59 |
+
inputs=[txt_input, mic_input, state],
|
| 60 |
+
outputs=[txt_input, state]
|
| 61 |
).then(
|
| 62 |
chat_fn,
|
| 63 |
+
inputs=[txt_input, state],
|
| 64 |
outputs=[chatbot, state]
|
| 65 |
)
|
| 66 |
|
| 67 |
+
# Handle uploads
|
| 68 |
uploader.upload(handle_upload, inputs=uploader, outputs=upload_status)
|
| 69 |
|
| 70 |
demo.launch()
|