Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,84 +1,51 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
-
import tempfile
|
| 4 |
-
import os
|
| 5 |
|
| 6 |
-
# --- Core AI Model Setup ---
|
| 7 |
pipe = pipeline("text-generation", model="Hulk810154/Kai", trust_remote_code=True)
|
| 8 |
|
| 9 |
-
|
| 10 |
-
def chat_fn(message, history=None):
|
| 11 |
-
# AGI-style persistent conversation
|
| 12 |
-
if history is None:
|
| 13 |
-
history = []
|
| 14 |
prompt = ""
|
| 15 |
for user, bot in history:
|
| 16 |
prompt += f"User: {user}\nAI: {bot}\n"
|
| 17 |
prompt += f"User: {message}\nAI:"
|
| 18 |
-
|
| 19 |
-
reply =
|
| 20 |
return reply
|
| 21 |
|
| 22 |
-
# --- Voice Input (Speech-to-Text) ---
|
| 23 |
-
def voice_to_text(audio):
|
| 24 |
-
# Uses gradio's built-in whisper-based speech recognition
|
| 25 |
-
return audio
|
| 26 |
-
|
| 27 |
-
# --- Text-to-Speech (TTS) ---
|
| 28 |
-
def tts_fn(text):
|
| 29 |
-
return text # Gradio auto-handles text-to-speech with the 'audio' output type
|
| 30 |
-
|
| 31 |
-
# --- File Upload Handler ---
|
| 32 |
def file_upload(files):
|
| 33 |
-
|
| 34 |
-
for file in files:
|
| 35 |
-
file_info.append(f"Uploaded: {os.path.basename(file.name)} ({file.size//1024} KB)")
|
| 36 |
-
return "\n".join(file_info)
|
| 37 |
-
|
| 38 |
-
# --- Screenshot Upload (Pseudo-Screen-Share) ---
|
| 39 |
-
def screenshot_upload(img):
|
| 40 |
-
return "Screenshot received!"
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
gr.Markdown(
|
| 45 |
-
"# 🧠 **Kai AGI Chat**\n"
|
| 46 |
-
"Live voice + text, upload files/images, and more. Powered by Hugging Face."
|
| 47 |
-
)
|
| 48 |
with gr.Row():
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
screenshot = gr.Image(label="Upload Screenshot")
|
| 79 |
-
screenshot_status = gr.Textbox(label="Status")
|
| 80 |
-
screenshot.upload(screenshot_upload, inputs=screenshot, outputs=screenshot_status)
|
| 81 |
-
|
| 82 |
-
gr.Markdown("**Session Memory:** Your conversation will persist during this session for AGI-level context.")
|
| 83 |
|
| 84 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
|
|
|
|
|
|
| 3 |
|
|
|
|
| 4 |
pipe = pipeline("text-generation", model="Hulk810154/Kai", trust_remote_code=True)
|
| 5 |
|
| 6 |
+
def chat_fn(message, history):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
prompt = ""
|
| 8 |
for user, bot in history:
|
| 9 |
prompt += f"User: {user}\nAI: {bot}\n"
|
| 10 |
prompt += f"User: {message}\nAI:"
|
| 11 |
+
output = pipe(prompt, max_length=256, do_sample=True)
|
| 12 |
+
reply = output[0]["generated_text"][len(prompt):].strip()
|
| 13 |
return reply
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
def file_upload(files):
|
| 16 |
+
return f"Received {len(files)} files."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
with gr.Blocks() as demo:
|
| 19 |
+
gr.Markdown("# Kai AGI – Text, Voice, & Multimodal Chat\n**Text, speak, or upload files/images. True AGI context.**")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
with gr.Row():
|
| 21 |
+
with gr.Column(scale=2):
|
| 22 |
+
chatbot = gr.Chatbot()
|
| 23 |
+
msg = gr.Textbox(label="Type a message or use voice...", placeholder="Type here...", scale=2)
|
| 24 |
+
voice_input = gr.Audio(source="microphone", type="filepath", label="🎤 Voice input (click mic)")
|
| 25 |
+
send_btn = gr.Button("Send", elem_id="send-btn")
|
| 26 |
+
with gr.Column(scale=1):
|
| 27 |
+
upload = gr.File(file_count="multiple", label="📎 Upload files/images")
|
| 28 |
+
upload_status = gr.Textbox(label="Upload status")
|
| 29 |
+
state = gr.State([])
|
| 30 |
+
|
| 31 |
+
def user_message(text, audio, history):
|
| 32 |
+
if text:
|
| 33 |
+
return text, history
|
| 34 |
+
if audio:
|
| 35 |
+
import whisper
|
| 36 |
+
model = whisper.load_model("tiny")
|
| 37 |
+
result = model.transcribe(audio)
|
| 38 |
+
return result["text"], history
|
| 39 |
+
return "", history
|
| 40 |
+
|
| 41 |
+
def respond(message, history):
|
| 42 |
+
reply = chat_fn(message, history)
|
| 43 |
+
history = history + [(message, reply)]
|
| 44 |
+
return history, history
|
| 45 |
+
|
| 46 |
+
send_btn.click(user_message, inputs=[msg, voice_input, state], outputs=[msg, state], queue=False).then(
|
| 47 |
+
respond, inputs=[msg, state], outputs=[chatbot, state]
|
| 48 |
+
)
|
| 49 |
+
upload.upload(file_upload, inputs=upload, outputs=upload_status)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
demo.launch()
|