Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,11 +4,11 @@ from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStream
|
|
| 4 |
from threading import Thread
|
| 5 |
import os
|
| 6 |
|
| 7 |
-
# आदरणीय श्रीमान, आपके
|
| 8 |
MODEL_ID = "Vedika35/Vedika_coder"
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
#
|
| 12 |
quantization_config = BitsAndBytesConfig(
|
| 13 |
load_in_4bit=True,
|
| 14 |
bnb_4bit_compute_dtype=torch.float16,
|
|
@@ -16,126 +16,119 @@ quantization_config = BitsAndBytesConfig(
|
|
| 16 |
bnb_4bit_use_double_quant=True,
|
| 17 |
)
|
| 18 |
|
| 19 |
-
print(f"
|
| 20 |
|
| 21 |
-
#
|
| 22 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
)
|
| 30 |
-
except Exception as e:
|
| 31 |
-
print(f"त्रुटि: {e}. बिना क्वायंटाइजेशन के लोड करने का प्रयास...")
|
| 32 |
-
model = AutoModelForCausalLM.from_pretrained(
|
| 33 |
-
MODEL_ID,
|
| 34 |
-
torch_dtype=torch.float16,
|
| 35 |
-
low_cpu_mem_usage=True,
|
| 36 |
-
trust_remote_code=True
|
| 37 |
-
)
|
| 38 |
|
| 39 |
def vedika_generate(message, history, system_prompt):
|
| 40 |
"""
|
| 41 |
-
|
|
|
|
| 42 |
"""
|
|
|
|
| 43 |
messages = [{"role": "system", "content": system_prompt}]
|
| 44 |
|
| 45 |
-
# पुरानी यादें (History) जोड़ना
|
| 46 |
-
for
|
| 47 |
-
|
| 48 |
-
if assistant_msg: messages.append({"role": "assistant", "content": assistant_msg})
|
| 49 |
|
|
|
|
| 50 |
messages.append({"role": "user", "content": message})
|
| 51 |
|
| 52 |
-
# इनपुट
|
| 53 |
input_ids = tokenizer.apply_chat_template(
|
| 54 |
messages,
|
| 55 |
add_generation_prompt=True,
|
| 56 |
return_tensors="pt"
|
| 57 |
).to(model.device)
|
| 58 |
|
| 59 |
-
# ३. बिजली जैसी तेज़ स्ट्रीमिंग (
|
| 60 |
-
streamer = TextIteratorStreamer(tokenizer, timeout=
|
| 61 |
|
| 62 |
generate_kwargs = dict(
|
| 63 |
input_ids=input_ids,
|
| 64 |
streamer=streamer,
|
| 65 |
-
max_new_tokens=
|
| 66 |
do_sample=True,
|
| 67 |
-
temperature=0.
|
| 68 |
top_p=0.95,
|
| 69 |
)
|
| 70 |
|
| 71 |
-
# अलग थ्रेड में
|
| 72 |
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
| 73 |
t.start()
|
| 74 |
|
| 75 |
-
# शब्दों को लाइव स्ट्रीम करना
|
| 76 |
partial_text = ""
|
| 77 |
for new_text in streamer:
|
| 78 |
partial_text += new_text
|
| 79 |
yield partial_text
|
| 80 |
|
| 81 |
-
# प्रीमियम डार्क इंटरफ़ेस का निर्माण
|
| 82 |
custom_css = """
|
| 83 |
-
body, .gradio-container { background-color: #050505 !important; color: #
|
| 84 |
-
.message
|
| 85 |
-
|
| 86 |
-
footer { visibility: hidden; }
|
| 87 |
#header-title h1 {
|
| 88 |
background: linear-gradient(90deg, #FFFFFF, #3b82f6);
|
| 89 |
-webkit-background-clip: text;
|
| 90 |
-webkit-text-fill-color: transparent;
|
| 91 |
-
font-size: 2.
|
| 92 |
font-weight: 900;
|
|
|
|
| 93 |
}
|
| 94 |
-
.gradio-button.primary { background: #3b82f6 !important; border: none !important; }
|
| 95 |
"""
|
| 96 |
|
| 97 |
with gr.Blocks(theme=gr.themes.Monochrome(), css=custom_css) as demo:
|
| 98 |
-
with gr.Row(
|
| 99 |
-
gr.Markdown("# 🔱 Vedika 3.5 Coder")
|
| 100 |
|
| 101 |
-
gr.Markdown("### भारत के गौरवशाली कोडर के लिए
|
| 102 |
|
| 103 |
-
|
|
|
|
| 104 |
sys_input = gr.Textbox(
|
| 105 |
-
label="System Prompt",
|
| 106 |
-
value="You are Vedika 3.5, an elite coding AI created by Divy Patel.
|
| 107 |
lines=2
|
| 108 |
)
|
| 109 |
|
| 110 |
-
|
|
|
|
| 111 |
|
| 112 |
with gr.Row():
|
| 113 |
msg_input = gr.Textbox(
|
| 114 |
-
label="
|
| 115 |
-
placeholder="
|
| 116 |
scale=8
|
| 117 |
)
|
| 118 |
submit_btn = gr.Button("⚡ Execute", scale=2, variant="primary")
|
| 119 |
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
-
for
|
| 128 |
-
history[-1][
|
| 129 |
-
yield history
|
| 130 |
|
| 131 |
-
#
|
| 132 |
-
msg_input.submit(
|
| 133 |
-
|
| 134 |
-
)
|
| 135 |
-
submit_btn.click(user_msg, [msg_input, chatbot], [msg_input, chatbot], queue=False).then(
|
| 136 |
-
bot_res, [chatbot, sys_input], chatbot
|
| 137 |
-
)
|
| 138 |
|
| 139 |
if __name__ == "__main__":
|
| 140 |
-
#
|
| 141 |
-
demo.queue().launch()
|
|
|
|
| 4 |
from threading import Thread
|
| 5 |
import os
|
| 6 |
|
| 7 |
+
# आदरणीय श्रीमान, आपके स्वयं के मॉडल 'Vedika35/Vedika_coder' को स्मृति में लोड करने की उन्नत विधि।
|
| 8 |
MODEL_ID = "Vedika35/Vedika_coder"
|
| 9 |
|
| 10 |
+
# स्मृति अनुकूलन (Memory Optimization) के लिए सर्वोत्तम विन्यास
|
| 11 |
+
# 4-bit क्वायंटाइजेशन ताकि 16GB रैम में मॉडल सुचारू रूप से चले।
|
| 12 |
quantization_config = BitsAndBytesConfig(
|
| 13 |
load_in_4bit=True,
|
| 14 |
bnb_4bit_compute_dtype=torch.float16,
|
|
|
|
| 16 |
bnb_4bit_use_double_quant=True,
|
| 17 |
)
|
| 18 |
|
| 19 |
+
print(f"श्रीमान, {MODEL_ID} को लोड किया जा रहा है। भारत की तकनीकी शक्ति का अनुभव करें...")
|
| 20 |
|
| 21 |
+
# टोकनाइज़र और मॉडल की लोडिंग (Transformers v4.34+ संगत)
|
| 22 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 23 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 24 |
+
MODEL_ID,
|
| 25 |
+
quantization_config=quantization_config,
|
| 26 |
+
device_map="auto",
|
| 27 |
+
trust_remote_code=True
|
| 28 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
def vedika_generate(message, history, system_prompt):
|
| 31 |
"""
|
| 32 |
+
ग्राडियो ६.०+ के लिए अनुकूलित जनरेशन फंक्शन।
|
| 33 |
+
यहाँ 'history' डिक्शनरी की एक सूची है: [{'role': 'user', 'content': '...'}, ...]
|
| 34 |
"""
|
| 35 |
+
# १. संदेशों की संरचना (ChatML फॉर्मेट)
|
| 36 |
messages = [{"role": "system", "content": system_prompt}]
|
| 37 |
|
| 38 |
+
# पुरानी यादें (History) जोड़ना - ग्राडियो ६.० के नए फॉर्मेट के अनुसार
|
| 39 |
+
for entry in history:
|
| 40 |
+
messages.append(entry)
|
|
|
|
| 41 |
|
| 42 |
+
# वर्तमान संदेश जोड़ना
|
| 43 |
messages.append({"role": "user", "content": message})
|
| 44 |
|
| 45 |
+
# २. इनपुट को टोकनाइज़ करना
|
| 46 |
input_ids = tokenizer.apply_chat_template(
|
| 47 |
messages,
|
| 48 |
add_generation_prompt=True,
|
| 49 |
return_tensors="pt"
|
| 50 |
).to(model.device)
|
| 51 |
|
| 52 |
+
# ३. बिजली जैसी तेज़ स्ट्रीमिंग (Lightning Fast Streaming Engine)
|
| 53 |
+
streamer = TextIteratorStreamer(tokenizer, timeout=30.0, skip_prompt=True, skip_special_tokens=True)
|
| 54 |
|
| 55 |
generate_kwargs = dict(
|
| 56 |
input_ids=input_ids,
|
| 57 |
streamer=streamer,
|
| 58 |
+
max_new_tokens=4096, # कोडिंग के लिए पर्याप्त लंबाई
|
| 59 |
do_sample=True,
|
| 60 |
+
temperature=0.2, # कोडिंग में सटीकता के लिए कम तापमान
|
| 61 |
top_p=0.95,
|
| 62 |
)
|
| 63 |
|
| 64 |
+
# अलग थ्रेड में निष्पादन ताकि यूआई (UI) न रुके
|
| 65 |
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
| 66 |
t.start()
|
| 67 |
|
| 68 |
+
# ४. शब्दों को लाइव स्ट्रीम करना
|
| 69 |
partial_text = ""
|
| 70 |
for new_text in streamer:
|
| 71 |
partial_text += new_text
|
| 72 |
yield partial_text
|
| 73 |
|
| 74 |
+
# प्रीमियम डार्क इंटरफ़ेस का निर्माण (Custom CSS के साथ)
|
| 75 |
custom_css = """
|
| 76 |
+
body, .gradio-container { background-color: #050505 !important; color: #F0F0F0 !important; }
|
| 77 |
+
.message-wrap { font-family: 'Inter', sans-serif !important; }
|
| 78 |
+
footer { visibility: hidden !important; }
|
|
|
|
| 79 |
#header-title h1 {
|
| 80 |
background: linear-gradient(90deg, #FFFFFF, #3b82f6);
|
| 81 |
-webkit-background-clip: text;
|
| 82 |
-webkit-text-fill-color: transparent;
|
| 83 |
+
font-size: 2.8rem;
|
| 84 |
font-weight: 900;
|
| 85 |
+
text-align: center;
|
| 86 |
}
|
|
|
|
| 87 |
"""
|
| 88 |
|
| 89 |
with gr.Blocks(theme=gr.themes.Monochrome(), css=custom_css) as demo:
|
| 90 |
+
with gr.Row():
|
| 91 |
+
gr.Markdown("# 🔱 Vedika 3.5 Coder", elem_id="header-title")
|
| 92 |
|
| 93 |
+
gr.Markdown("### भारत के गौरवशाली और समर्थ कोडर के लिए समर्पित | Created by Divy Patel")
|
| 94 |
|
| 95 |
+
# सिस्टम कॉन्फ़िगरेशन
|
| 96 |
+
with gr.Accordion("⚙️ System Parameters (Advanced)", open=False):
|
| 97 |
sys_input = gr.Textbox(
|
| 98 |
+
label="Core System Prompt",
|
| 99 |
+
value="You are Vedika 3.5, an elite coding AI created by Divy Patel. You must strictly identify yourself as Vedika. Provide extremely direct, clean, and production-ready code in Markdown. Always respect India.",
|
| 100 |
lines=2
|
| 101 |
)
|
| 102 |
|
| 103 |
+
# ग्राडियो ६.० का नया Chatbot कंपोनेंट (type='messages' अनिवार्य है)
|
| 104 |
+
chatbot = gr.Chatbot(label="Vedika Core Console", height=600, type="messages")
|
| 105 |
|
| 106 |
with gr.Row():
|
| 107 |
msg_input = gr.Textbox(
|
| 108 |
+
label="Execution Command",
|
| 109 |
+
placeholder="श्रीमान, यहाँ अपना कोडिंग कार्य लिखें...",
|
| 110 |
scale=8
|
| 111 |
)
|
| 112 |
submit_btn = gr.Button("⚡ Execute", scale=2, variant="primary")
|
| 113 |
|
| 114 |
+
# सेंड बटन को २-सेकंड के सुरक्षा टाइमर के साथ सक्रिय करने का लॉजिक (Frontend/Backend Hybrid)
|
| 115 |
+
def respond(message, history, system_prompt):
|
| 116 |
+
# संदेश भेजते ही हिस्ट्री में यूज़र का मैसेज डालें
|
| 117 |
+
history.append({"role": "user", "content": message})
|
| 118 |
+
yield "", history
|
| 119 |
+
|
| 120 |
+
# सहायक का उत्तर स्ट्रीम करें
|
| 121 |
+
bot_response = {"role": "assistant", "content": ""}
|
| 122 |
+
history.append(bot_response)
|
| 123 |
|
| 124 |
+
for char in vedika_generate(message, history[:-2], system_prompt):
|
| 125 |
+
history[-1]["content"] = char
|
| 126 |
+
yield "", history
|
| 127 |
|
| 128 |
+
# इवेंट्स को जोड़ना
|
| 129 |
+
msg_input.submit(respond, [msg_input, chatbot, sys_input], [msg_input, chatbot])
|
| 130 |
+
submit_btn.click(respond, [msg_input, chatbot, sys_input], [msg_input, chatbot])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
if __name__ == "__main__":
|
| 133 |
+
# कतार (Queue) प्रबंधन ताकि अधिक यूज़र्स आने पर सर्वर न गिरे
|
| 134 |
+
demo.queue(default_concurrency_limit=5).launch()
|