Mhammad-Raller commited on
Commit
366ac6c
·
verified ·
1 Parent(s): 76a525b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -21
app.py CHANGED
@@ -6,41 +6,31 @@
6
  import gradio as gr
7
  from transformers import pipeline
8
 
9
- # Load the SmolLM 1.7B Instruct model
10
  generator = pipeline("text-generation", model="HuggingFaceTB/SmolLM-1.7B-Instruct")
11
 
12
  def chat_respond(message, history):
13
- # Format the modern message list history into text for SmolLM
14
  prompt = ""
15
- for msg in history:
16
- role_label = "User" if msg["role"] == "user" else "Assistant"
17
- prompt += f"{role_label}: {msg['content']}\n"
18
  prompt += f"User: {message}\nAssistant:"
19
 
20
- # Generate the text tokens
21
  results = generator(prompt, max_new_tokens=150, do_sample=True, temperature=0.7)
22
 
23
- # Clean up the output string
24
  full_text = results['generated_text']
25
  response = full_text.split("Assistant:")[-1].strip()
26
-
27
- # Return history with the new assistant message appended
28
- history.append({"role": "user", "content": message})
29
- history.append({"role": "assistant", "content": response})
30
- return "", history
31
 
32
- # Build a clean, modern Chat interface utilizing Gradio 6 styling
33
  with gr.Blocks() as demo:
34
  gr.Markdown("# 🐨 Raller Magnium AI")
35
  gr.Markdown("Welcome to our small AI system, hope you like it. Powered by raller.pro.")
36
 
37
- # Modern Gradio 6 chatbot component setup with message system format
38
- chatbot = gr.Chatbot(type="messages")
39
- msg_input = gr.Textbox(placeholder="Ask Raller Magnium anything...", container=False, scale=7)
40
-
41
- # Handle the submission loop cleanly
42
- msg_input.submit(chat_respond, inputs=[msg_input, chatbot], outputs=[msg_input, chatbot])
43
 
44
- # Pass theme variables to launch directly per Gradio 6 requirements
45
  demo.launch(theme=gr.themes.Soft())
46
-
 
6
  import gradio as gr
7
  from transformers import pipeline
8
 
9
+ # Load the SmolLM 1.7B Instruct model cleanly
10
  generator = pipeline("text-generation", model="HuggingFaceTB/SmolLM-1.7B-Instruct")
11
 
12
  def chat_respond(message, history):
13
+ # Format basic text history for the AI engine
14
  prompt = ""
15
+ for user_msg, ai_msg in history:
16
+ prompt += f"User: {user_msg}\nAssistant: {ai_msg}\n"
 
17
  prompt += f"User: {message}\nAssistant:"
18
 
19
+ # Generate response string tokens
20
  results = generator(prompt, max_new_tokens=150, do_sample=True, temperature=0.7)
21
 
22
+ # Extract only the text that follows the final Assistant label
23
  full_text = results['generated_text']
24
  response = full_text.split("Assistant:")[-1].strip()
25
+ return response
 
 
 
 
26
 
27
+ # Create the clean, error-free chatbot block layout
28
  with gr.Blocks() as demo:
29
  gr.Markdown("# 🐨 Raller Magnium AI")
30
  gr.Markdown("Welcome to our small AI system, hope you like it. Powered by raller.pro.")
31
 
32
+ # The universal ChatInterface engine built into Gradio
33
+ gr.ChatInterface(fn=chat_respond)
 
 
 
 
34
 
35
+ # Launch with the soft UI theme cleanly applied
36
  demo.launch(theme=gr.themes.Soft())