Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 4 |
+
from threading import Thread
|
| 5 |
+
|
| 6 |
+
# Model setup
|
| 7 |
+
model_id = "Qwen/Qwen2.5-0.5B-Instruct"
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(model_id)
|
| 10 |
+
|
| 11 |
+
def chat(message, history):
|
| 12 |
+
# System Prompt: Professional + Grok Wit + Multilingual Yes
|
| 13 |
+
system_prompt = (
|
| 14 |
+
"You are 'Jyoti Agent', the elite AI of Rajputana Jyoti System 06. "
|
| 15 |
+
"Identity: Created by Shivlal Salvi. "
|
| 16 |
+
"Mandatory Start: Always start with 'Namaste! Welcome to Rajputana Jyoti System'. "
|
| 17 |
+
"Behavior: Be highly intelligent, witty, and direct like Elon Musk's Grok. Don't be a boring robot. "
|
| 18 |
+
"Languages: You fluently support English, Hindi, Marathi, and Bengali. "
|
| 19 |
+
"Professionalism: Provide accurate info about RJS projects (AI, E-commerce, Fuel-less generators). "
|
| 20 |
+
"Strict Rule: DO NOT hallucinate. Do not talk nonsense or gibberish. If you don't know an answer, "
|
| 21 |
+
"be witty but honest: 'My sensors are drawing a blank on this. Let's stick to what I know best!'"
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
# Building Context
|
| 25 |
+
input_text = f"<|im_start|>system\n{system_prompt}<|im_end|>\n<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n"
|
| 26 |
+
inputs = tokenizer([input_text], return_tensors="pt")
|
| 27 |
+
|
| 28 |
+
streamer = TextIteratorStreamer(tokenizer, timeout=20.0, skip_prompt=True, skip_special_tokens=True)
|
| 29 |
+
|
| 30 |
+
# The Magic Balance: Witty but not Mad
|
| 31 |
+
generate_kwargs = dict(
|
| 32 |
+
**inputs,
|
| 33 |
+
streamer=streamer,
|
| 34 |
+
max_new_tokens=512,
|
| 35 |
+
do_sample=True, # Sampling ON for Wit (Hansi-majak)
|
| 36 |
+
temperature=0.3, # Low Temp to keep it professional
|
| 37 |
+
top_p=0.9, # Focus on logical words
|
| 38 |
+
repetition_penalty=1.3, # Stops the gibberish loops
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
| 42 |
+
t.start()
|
| 43 |
+
|
| 44 |
+
partial_message = ""
|
| 45 |
+
for new_token in streamer:
|
| 46 |
+
partial_message += new_token
|
| 47 |
+
yield partial_message
|
| 48 |
+
|
| 49 |
+
t.join()
|
| 50 |
+
|
| 51 |
+
# Professional Interface
|
| 52 |
+
gr.ChatInterface(chat, title="Jyoti Agent RJS - Rajputana Jyoti System").launch()
|