Shivlal6660 commited on
Commit
23d4d76
·
verified ·
1 Parent(s): 820a4c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -18
app.py CHANGED
@@ -3,54 +3,54 @@ import torch
3
  from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
4
  from threading import Thread
5
 
6
- # 1.5B Model: Smart enough to understand, light enough to run stable
7
  model_id = "Qwen/Qwen2.5-1.5B-Instruct"
8
  tokenizer = AutoTokenizer.from_pretrained(model_id)
9
  model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, device_map="auto")
10
 
11
  def chat(message, history):
12
- # IDENTITY & MULTI-SECTOR MASTER PROMPT
13
  system_prompt = (
14
- "Your name is 'Jyoti Agent', official AI of Rajputana Jyoti System 06 (OPC) Private Limited. "
15
- "Identity: Created ONLY by Shivlal Salvi. No other creators exist. "
16
- "Strict Rule: Do not hallucinate. Do not mention 'Yashraj' or 'Dr. Ajit Jain'. "
17
- "Sectors: You are an expert in Agriculture, Business, Tech, and Rural development. "
 
18
  "Behavior: Be witty like Grok, maintain Rajputana pride, and analyze user emotions. "
19
- "Language: Speak Hinglish, Hindi, Marathi, and Bengali. "
20
  "Mandatory Start: Always begin with 'Namaste! Welcome to Rajputana Jyoti System'."
21
  )
22
 
23
- # Context management for memory
24
  full_prompt = f"<|im_start|>system\n{system_prompt}<|im_end|>\n"
25
- for turn in history:
26
- full_prompt += f"<|im_start|>user\n{turn[0]}<|im_end|>\n<|im_start|>assistant\n{turn[1]}<|im_end|>\n"
 
 
27
  full_prompt += f"<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n"
28
 
29
  inputs = tokenizer([full_prompt], return_tensors="pt").to(model.device)
30
 
31
- # Fast Streamer
32
  streamer = TextIteratorStreamer(tokenizer, timeout=30.0, skip_prompt=True, skip_special_tokens=True)
33
 
34
- # ANTI-HALLUCINATION SETTINGS
35
  generate_kwargs = dict(
36
  **inputs,
37
  streamer=streamer,
38
  max_new_tokens=512,
39
- do_sample=False, # Greedy decoding: No 'random' words allowed
40
- repetition_penalty=1.5, # Stops the AI from getting stuck in loops
41
  )
42
 
43
  t = Thread(target=model.generate, kwargs=generate_kwargs)
44
  t.start()
45
 
46
- # Streaming the output
47
  partial_message = ""
48
  for new_token in streamer:
49
  if new_token:
50
  partial_message += new_token
51
- yield partial_message
52
 
53
  t.join()
54
 
55
- # Professional Interface
56
- gr.ChatInterface(chat, title="🚩 Jyoti Agent RJS - Balanced Intelligence").launch()
 
3
  from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
4
  from threading import Thread
5
 
6
+ # Smart & Stable 1.5B Model - Identity Locked
7
  model_id = "Qwen/Qwen2.5-1.5B-Instruct"
8
  tokenizer = AutoTokenizer.from_pretrained(model_id)
9
  model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16, device_map="auto")
10
 
11
  def chat(message, history):
12
+ # BRAND IDENTITY: Rajputana Jyoti System 06 (OPC) Private Limited
13
  system_prompt = (
14
+ "You are 'Jyoti Agent', the official AI of Rajputana Jyoti System 06 (OPC) Private Limited. "
15
+ "Identity: Created ONLY by Shivlal Salvi. You are the brand voice of RJ 06. "
16
+ "Strict Rule: Do not hallucinate. Do not mention any other creator. "
17
+ "Sectors: Expert in Agriculture, Business, and Tech for all people. "
18
+ "Language: Speak in professional Hinglish, Marthi, Bangali (Mix of Hindi & English). "
19
  "Behavior: Be witty like Grok, maintain Rajputana pride, and analyze user emotions. "
 
20
  "Mandatory Start: Always begin with 'Namaste! Welcome to Rajputana Jyoti System'."
21
  )
22
 
23
+ # Clean History Handling to avoid KeyError
24
  full_prompt = f"<|im_start|>system\n{system_prompt}<|im_end|>\n"
25
+ if history:
26
+ for user_msg, bot_msg in history:
27
+ full_prompt += f"<|im_start|>user\n{user_msg}<|im_end|>\n<|im_start|>assistant\n{bot_msg}<|im_end|>\n"
28
+
29
  full_prompt += f"<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n"
30
 
31
  inputs = tokenizer([full_prompt], return_tensors="pt").to(model.device)
32
 
33
+ # STREAMING ON: Har shabd live aayega
34
  streamer = TextIteratorStreamer(tokenizer, timeout=30.0, skip_prompt=True, skip_special_tokens=True)
35
 
 
36
  generate_kwargs = dict(
37
  **inputs,
38
  streamer=streamer,
39
  max_new_tokens=512,
40
+ do_sample=False, # Sachai ke liye Greedy Decoding
41
+ repetition_penalty=1.5,
42
  )
43
 
44
  t = Thread(target=model.generate, kwargs=generate_kwargs)
45
  t.start()
46
 
 
47
  partial_message = ""
48
  for new_token in streamer:
49
  if new_token:
50
  partial_message += new_token
51
+ yield partial_message # Real-time stream
52
 
53
  t.join()
54
 
55
+ # Professional Brand Interface
56
+ gr.ChatInterface(chat, title="🚩 Jyoti Agent RJS - Brand Official").launch()