Hulk810154 commited on
Commit
20b9745
Β·
verified Β·
1 Parent(s): 58e1f7c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -25
app.py CHANGED
@@ -1,62 +1,70 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
- # β€”β€”β€” Load Pipelines β€”β€”β€”
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
- # β€”β€”β€” Handlers β€”β€”β€”
16
  def chat_fn(message, history):
17
- prompt = ""
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 speech_to_text(audio_path):
27
- if not audio_path:
 
28
  return ""
29
- return asr_pipe(audio_path)["text"]
 
 
 
 
 
30
 
31
  def handle_upload(files):
32
- return f"Received {len(files)} file(s): " + ", ".join([f.name for f in files])
 
33
 
34
- # β€”β€”β€” UI β€”β€”β€”
35
- with gr.Blocks() as demo:
36
- gr.Markdown("# 🧠 Kai AGI Chat\n**Type, Speak or Upload β€” true multimodal AGI.**")
37
- state = gr.State([])
38
 
39
  with gr.Row():
40
  with gr.Column():
41
- chatbot = gr.Chatbot(label="Conversation")
42
- txt = gr.Textbox(placeholder="Type here…", label="Text Input")
43
- mic = gr.Audio(source="microphone", type="filepath", label="🎀 Voice Input")
44
- send = gr.Button("Send")
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
- # Wire it up
50
- send.click(
51
- lambda txt, mic, history: ( speech_to_text(mic) or txt, history ),
52
- inputs=[txt, mic, state],
53
- outputs=[txt, state]
54
  ).then(
55
  chat_fn,
56
- inputs=[txt, state],
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()