saiful-ai-dev commited on
Commit
ac69158
·
verified ·
1 Parent(s): 375f784

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -21
app.py CHANGED
@@ -1,35 +1,46 @@
1
  import gradio as gr
2
- from transformers import pipeline
3
- import torch
4
 
5
- # ৩বি মডেল লোড করার সবথেকে নিরাপদ উপায়
6
- model_id = "Qwen/Qwen2.5-3B-Instruct"
7
-
8
- print("⏳ এআই টিউটর লোড হচ্ছে... এটি ২-৩ মিনিট সময় নেবে।")
 
9
 
10
- # মেমোরি বাঁচানোর জন্য বিশেষ সেটিংস
11
- pipe = pipeline(
12
- "text-generation",
13
- model=model_id,
14
- model_kwargs={"torch_dtype": torch.float32, "low_cpu_mem_usage": True},
15
- device_map="auto"
16
  )
17
 
18
  def respond(message, history):
19
- messages = [
20
- {"role": "system", "content": "You are Motion Mind X, a friendly SSC/HSC tutor from Bangladesh. Respond clearly in Bengali/English."},
21
- {"role": "user", "content": message},
22
- ]
 
 
 
 
 
 
23
 
24
- # জেনারেশন শুরু
25
- out = pipe(messages, max_new_tokens=512)
26
- return out[0]['generated_text'][-1]['content']
 
 
 
27
 
28
- # চ্যাট ইন্টারফেস
29
  demo = gr.ChatInterface(
30
  respond,
31
  title="Motion Mind X 🚀",
32
- examples=["SSC গণিত প্রস্তুতি কীভাবে নেব?", "HSC English 2nd paper grammar help"]
 
33
  )
34
 
35
  if __name__ == "__main__":
 
1
  import gradio as gr
2
+ from huggingface_hub import hf_hub_download
3
+ from llama_cpp import Llama
4
 
5
+ # ১. মডেল ডাউনলোড
6
+ model_path = hf_hub_download(
7
+ repo_id="saiful-ai-dev/MotionMindX",
8
+ filename="Qwen2.5-3B-Instruct-Q4_K_M.gguf"
9
+ )
10
 
11
+ # ২. মডেল সেটিংস (তোমার চাওয়া অনুযায়ী ২০৪৮ সেট করা হয়েছে)
12
+ llm = Llama(
13
+ model_path=model_path,
14
+ n_ctx=2048, # Context window
15
+ n_batch=512, # RAM বাঁচাতে ব্যাচ সাইজ একটু কম রাখা ভালো, তবে প্রম্পট ২০৪৮ হ্যান্ডেল করবে
16
+ n_threads=2 # Free CPU এর জন্য এটাই সেরা
17
  )
18
 
19
  def respond(message, history):
20
+ # প্রম্পট ফরম্যাটিং
21
+ prompt = f"<|im_start|>system\nYou are Motion Mind X, a helpful tutor for SSC/HSC students in Bangladesh. Respond clearly.<|im_end|>\n<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n"
22
+
23
+ # ৩. স্ট্রিমিং (Streaming) শুরু
24
+ response_stream = llm(
25
+ prompt,
26
+ max_tokens=512,
27
+ stop=["<|im_end|>"],
28
+ stream=True # এটি লেখাকে এক এক করে পাঠাবে
29
+ )
30
 
31
+ partial_message = ""
32
+ for chunk in response_stream:
33
+ if "text" in chunk["choices"][0]:
34
+ token = chunk["choices"][0]["text"]
35
+ partial_message += token
36
+ yield partial_message # এটিই স্ট্রিমিং ম্যাজিক!
37
 
38
+ # ৪. ইন্টারফেস ডিজাইন
39
  demo = gr.ChatInterface(
40
  respond,
41
  title="Motion Mind X 🚀",
42
+ description="SSC/HSC শিক্ষার্থীদের জন্য লাইভ এআই টিউটর (Streaming Enabled)",
43
+ theme="soft"
44
  )
45
 
46
  if __name__ == "__main__":