Hulk810154 commited on
Commit
b939b98
·
verified ·
1 Parent(s): 89a913a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -68
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
- # --- Core Chat Functionality ---
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
- result = pipe(prompt, max_length=256, do_sample=True)
19
- reply = result[0]["generated_text"][len(prompt):].strip()
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
- file_info = []
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
- # --- AGI Customization ---
43
- with gr.Blocks(theme=gr.themes.Base()) as demo:
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
- persona = gr.Textbox(label="Agent Persona Prompt (Optional)", placeholder="e.g., Act as a business advisor...", interactive=True)
50
- session_history = gr.State([])
51
-
52
- with gr.Tab("💬 Chat"):
53
- chat = gr.ChatInterface(
54
- fn=chat_fn,
55
- additional_inputs=[persona],
56
- text_to_speech="auto",
57
- speech_to_text="whisper",
58
- retry_btn=None,
59
- undo_btn=None,
60
- title="Kai – AGI Chat",
61
- description="Text, talk, and upload files. True multimodal AGI mode. (Voice features require browser permission.)",
62
- )
63
-
64
- with gr.Tab("🎙️ Voice Chat"):
65
- mic = gr.Audio(source="microphone", type="filepath", label="Speak here")
66
- voice_output = gr.Textbox(label="Recognized Text")
67
- tts_button = gr.Button("Text-to-Speech")
68
- tts_audio = gr.Audio(label="AI Speaks")
69
- mic.change(lambda x: pipe(x, max_length=256, do_sample=True)[0]["generated_text"], mic, voice_output)
70
- tts_button.click(tts_fn, inputs=voice_output, outputs=tts_audio)
71
-
72
- with gr.Tab("📤 Upload File / Image"):
73
- file_input = gr.File(file_count="multiple")
74
- file_status = gr.Textbox(label="File status")
75
- file_input.upload(file_upload, inputs=file_input, outputs=file_status)
76
-
77
- with gr.Tab("🖥️ Share Screenshot"):
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()