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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -18
app.py CHANGED
@@ -10,31 +10,37 @@ from transformers import pipeline
10
  generator = pipeline("text-generation", model="HuggingFaceTB/SmolLM-1.7B-Instruct")
11
 
12
  def chat_respond(message, history):
13
- # Format the conversation history for the model
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
20
  results = generator(prompt, max_new_tokens=150, do_sample=True, temperature=0.7)
21
 
22
- # Extract only the newly generated text
23
  full_text = results['generated_text']
24
  response = full_text.split("Assistant:")[-1].strip()
25
- return response
 
 
 
 
26
 
27
- # Build a clean chatbot UI matching raller.pro
28
- with gr.Blocks(theme=gr.themes.Soft()) as demo:
29
- gr.Markdown("# 🤖 Raller Magnium AI")
30
- gr.Markdown("Welcome to the official open-source AI engine for raller.pro.")
 
 
 
 
31
 
32
- gr.ChatInterface(
33
- fn=chat_respond,
34
- textbox=gr.Textbox(placeholder="Ask Raller Magnium anything...", container=False, scale=7),
35
- retry_btn="🔄 Retry",
36
- undo_btn="↩️ Undo",
37
- clear_btn="🗑️ Clear Chat",
38
- )
39
 
40
- demo.launch()
 
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