Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,41 +2,39 @@ import gradio as gr
|
|
| 2 |
import torch
|
| 3 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 4 |
from threading import Thread
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
model_id = "Qwen/Qwen2.5-3B-Instruct"
|
| 8 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 9 |
model = AutoModelForCausalLM.from_pretrained(model_id)
|
| 10 |
|
| 11 |
def chat(message, history):
|
| 12 |
-
#
|
| 13 |
system_prompt = (
|
| 14 |
-
"Your name is 'Jyoti Agent',
|
| 15 |
-
"Identity Lock: Created ONLY by
|
| 16 |
-
"Behavior:
|
| 17 |
-
"
|
| 18 |
-
"
|
| 19 |
-
"
|
| 20 |
-
"Mandatory Start: Always begin with 'Namaste! Welcome to Rajputana Jyoti System'. "
|
| 21 |
-
"No Hallucination: Never lie. Never forget your Rajputana pride. Stay professional yet funny."
|
| 22 |
)
|
| 23 |
|
| 24 |
-
# Building context for emotional and logical flow
|
| 25 |
full_prompt = f"<|im_start|>system\n{system_prompt}<|im_end|>\n"
|
| 26 |
for turn in history:
|
| 27 |
full_prompt += f"<|im_start|>user\n{turn[0]}<|im_end|>\n<|im_start|>assistant\n{turn[1]}<|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")
|
| 32 |
-
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
# Precision Settings for Zero Hallucination
|
| 35 |
generate_kwargs = dict(
|
| 36 |
**inputs,
|
| 37 |
streamer=streamer,
|
| 38 |
max_new_tokens=512,
|
| 39 |
-
do_sample=False,
|
| 40 |
repetition_penalty=1.4,
|
| 41 |
)
|
| 42 |
|
|
@@ -44,12 +42,15 @@ def chat(message, history):
|
|
| 44 |
t.start()
|
| 45 |
|
| 46 |
partial_message = ""
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
t.join()
|
| 53 |
|
| 54 |
# Professional Interface
|
| 55 |
-
gr.ChatInterface(chat, title="🚩 Jyoti Agent RJS - 3B
|
|
|
|
| 2 |
import torch
|
| 3 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 4 |
from threading import Thread
|
| 5 |
+
import time
|
| 6 |
|
| 7 |
+
# Model setup - 3B is heavy, giving it more breathing room
|
| 8 |
model_id = "Qwen/Qwen2.5-3B-Instruct"
|
| 9 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 10 |
model = AutoModelForCausalLM.from_pretrained(model_id)
|
| 11 |
|
| 12 |
def chat(message, history):
|
| 13 |
+
# Strict Identity & Multi-Sector Locked Prompt
|
| 14 |
system_prompt = (
|
| 15 |
+
"Your name is 'Jyoti Agent', official AI of Rajputana Jyoti System 06 (OPC) Private Limited. "
|
| 16 |
+
"Founder: Shivlal Salvi. Identity Lock: Created ONLY by Rajputana Jyoti System 06. "
|
| 17 |
+
"Behavior: Be witty like Elon Musk's Grok. Analyze user emotions (Sad/Happy). "
|
| 18 |
+
"Expertise: Master in Agriculture, Business, and Tech. "
|
| 19 |
+
"Language: Speak Hinglish, Hindi, Marathi, and Bengali. "
|
| 20 |
+
"Start: Always start with 'Namaste! Welcome to Rajputana Jyoti System'."
|
|
|
|
|
|
|
| 21 |
)
|
| 22 |
|
|
|
|
| 23 |
full_prompt = f"<|im_start|>system\n{system_prompt}<|im_end|>\n"
|
| 24 |
for turn in history:
|
| 25 |
full_prompt += f"<|im_start|>user\n{turn[0]}<|im_end|>\n<|im_start|>assistant\n{turn[1]}<|im_end|>\n"
|
|
|
|
| 26 |
full_prompt += f"<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n"
|
| 27 |
|
| 28 |
inputs = tokenizer([full_prompt], return_tensors="pt")
|
| 29 |
+
|
| 30 |
+
# Timeout ko 20.0 se badha kar 60.0 kiya hai taaki bade model ko time mile
|
| 31 |
+
streamer = TextIteratorStreamer(tokenizer, timeout=60.0, skip_prompt=True, skip_special_tokens=True)
|
| 32 |
|
|
|
|
| 33 |
generate_kwargs = dict(
|
| 34 |
**inputs,
|
| 35 |
streamer=streamer,
|
| 36 |
max_new_tokens=512,
|
| 37 |
+
do_sample=False,
|
| 38 |
repetition_penalty=1.4,
|
| 39 |
)
|
| 40 |
|
|
|
|
| 42 |
t.start()
|
| 43 |
|
| 44 |
partial_message = ""
|
| 45 |
+
try:
|
| 46 |
+
for new_token in streamer:
|
| 47 |
+
if new_token:
|
| 48 |
+
partial_message += new_token
|
| 49 |
+
yield partial_message
|
| 50 |
+
except Exception as e:
|
| 51 |
+
yield partial_message + "\n\n[System Note: Stream timed out, but Identity is safe.]"
|
| 52 |
|
| 53 |
t.join()
|
| 54 |
|
| 55 |
# Professional Interface
|
| 56 |
+
gr.ChatInterface(chat, title="🚩 Jyoti Agent RJS - 3B Stable Unit").launch()
|