Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,11 +4,10 @@ from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStream
|
|
| 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,9 +15,9 @@ quantization_config = BitsAndBytesConfig(
|
|
| 16 |
bnb_4bit_use_double_quant=True,
|
| 17 |
)
|
| 18 |
|
| 19 |
-
print(f"श्रीमान, {MODEL_ID} को लोड किया जा रहा है।
|
| 20 |
|
| 21 |
-
# टोकनाइज़र और मॉडल की लोडिंग
|
| 22 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 23 |
model = AutoModelForCausalLM.from_pretrained(
|
| 24 |
MODEL_ID,
|
|
@@ -29,49 +28,50 @@ model = AutoModelForCausalLM.from_pretrained(
|
|
| 29 |
|
| 30 |
def vedika_generate(message, history, system_prompt):
|
| 31 |
"""
|
| 32 |
-
ग्राडियो
|
| 33 |
-
यहाँ 'history'
|
| 34 |
"""
|
| 35 |
-
# १. संदेशों की संरचना (ChatML फॉर्मेट)
|
| 36 |
messages = [{"role": "system", "content": system_prompt}]
|
| 37 |
|
| 38 |
-
# पुरानी यादें (History) जोड़ना
|
| 39 |
-
for
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
-
#
|
| 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 |
-
#
|
| 75 |
custom_css = """
|
| 76 |
body, .gradio-container { background-color: #050505 !important; color: #F0F0F0 !important; }
|
| 77 |
.message-wrap { font-family: 'Inter', sans-serif !important; }
|
|
@@ -86,49 +86,47 @@ footer { visibility: hidden !important; }
|
|
| 86 |
}
|
| 87 |
"""
|
| 88 |
|
| 89 |
-
|
|
|
|
| 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.
|
| 100 |
lines=2
|
| 101 |
)
|
| 102 |
|
| 103 |
-
#
|
| 104 |
-
chatbot = gr.Chatbot(label="Vedika Core Console", height=600
|
| 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(
|
| 118 |
yield "", history
|
| 119 |
|
| 120 |
-
|
| 121 |
-
|
| 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 |
-
#
|
| 134 |
-
demo.queue(default_concurrency_limit=5).launch(
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from threading import Thread
|
| 5 |
import os
|
| 6 |
|
| 7 |
+
# आदरणीय श्रीमान, आपके स्वयं के मॉडल 'Vedika35/Vedika_coder' को स्मृति में लोड करने की सुरक्षित विधि।
|
| 8 |
MODEL_ID = "Vedika35/Vedika_coder"
|
| 9 |
|
| 10 |
+
# स्मृति अनुकूलन (Memory Optimization) के लिए 4-bit क्वायंटाइजेशन
|
|
|
|
| 11 |
quantization_config = BitsAndBytesConfig(
|
| 12 |
load_in_4bit=True,
|
| 13 |
bnb_4bit_compute_dtype=torch.float16,
|
|
|
|
| 15 |
bnb_4bit_use_double_quant=True,
|
| 16 |
)
|
| 17 |
|
| 18 |
+
print(f"आदरणीय श्रीमान, {MODEL_ID} को लोड किया जा रहा है। कृपया प्रतीक्षा करें...")
|
| 19 |
|
| 20 |
+
# टोकनाइज़र और मॉडल की लोडिंग
|
| 21 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 22 |
model = AutoModelForCausalLM.from_pretrained(
|
| 23 |
MODEL_ID,
|
|
|
|
| 28 |
|
| 29 |
def vedika_generate(message, history, system_prompt):
|
| 30 |
"""
|
| 31 |
+
यह जनरेशन फंक्शन अब ग्राडियो के सबसे सुरक्षित और स्थिर फॉर्मेट (List of Lists) का उपयोग करता है।
|
| 32 |
+
यहाँ 'history' का फॉर्मेट है: [[user_msg, bot_msg], [user_msg, bot_msg], ...]
|
| 33 |
"""
|
|
|
|
| 34 |
messages = [{"role": "system", "content": system_prompt}]
|
| 35 |
|
| 36 |
+
# पुरानी यादें (History) जोड़ना
|
| 37 |
+
for user_msg, bot_msg in history:
|
| 38 |
+
if user_msg:
|
| 39 |
+
messages.append({"role": "user", "content": user_msg})
|
| 40 |
+
if bot_msg:
|
| 41 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 42 |
|
| 43 |
+
# वर्तमान संदेश
|
| 44 |
messages.append({"role": "user", "content": message})
|
| 45 |
|
| 46 |
+
# इनपुट को टोकनाइज़ करना
|
| 47 |
input_ids = tokenizer.apply_chat_template(
|
| 48 |
messages,
|
| 49 |
add_generation_prompt=True,
|
| 50 |
return_tensors="pt"
|
| 51 |
).to(model.device)
|
| 52 |
|
| 53 |
+
# बिजली जैसी तेज़ स्ट्रीमिंग (Lightning Fast Streaming)
|
| 54 |
streamer = TextIteratorStreamer(tokenizer, timeout=30.0, skip_prompt=True, skip_special_tokens=True)
|
| 55 |
|
| 56 |
generate_kwargs = dict(
|
| 57 |
input_ids=input_ids,
|
| 58 |
streamer=streamer,
|
| 59 |
+
max_new_tokens=4096,
|
| 60 |
do_sample=True,
|
| 61 |
+
temperature=0.2, # कोडिंग के लिए सटीकता
|
| 62 |
top_p=0.95,
|
| 63 |
)
|
| 64 |
|
|
|
|
| 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; }
|
|
|
|
| 86 |
}
|
| 87 |
"""
|
| 88 |
|
| 89 |
+
# ध्यान दें: ग्राडियो ६.० के अनुसार यहाँ से theme और css हटा दिए गए हैं
|
| 90 |
+
with gr.Blocks() as demo:
|
| 91 |
with gr.Row():
|
| 92 |
gr.Markdown("# 🔱 Vedika 3.5 Coder", elem_id="header-title")
|
| 93 |
|
| 94 |
gr.Markdown("### भारत के गौरवशाली और समर्थ कोडर के लिए समर्पित | Created by Divy Patel")
|
| 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. Identify only as Vedika. Provide extremely direct, clean, and production-ready code in Markdown. Always respect India.",
|
| 100 |
lines=2
|
| 101 |
)
|
| 102 |
|
| 103 |
+
# ध्यान दें: यहाँ से type="messages" हटा दिया गया है ताकि कोई TypeError न आए
|
| 104 |
+
chatbot = gr.Chatbot(label="Vedika Core Console", height=600)
|
| 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 |
def respond(message, history, system_prompt):
|
| 115 |
+
# सुरक्षित फॉर्मेट में हिस्ट्री अपडेट करना
|
| 116 |
+
history.append([message, ""])
|
| 117 |
yield "", history
|
| 118 |
|
| 119 |
+
for char in vedika_generate(message, history[:-1], system_prompt):
|
| 120 |
+
history[-1][1] = char
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
yield "", history
|
| 122 |
|
| 123 |
+
# इवेंट्स
|
| 124 |
msg_input.submit(respond, [msg_input, chatbot, sys_input], [msg_input, chatbot])
|
| 125 |
submit_btn.click(respond, [msg_input, chatbot, sys_input], [msg_input, chatbot])
|
| 126 |
|
| 127 |
if __name__ == "__main__":
|
| 128 |
+
# ध्यान दें: ग्राडियो ६.० की चेतावनी (Warning) के अनुसार theme और css को launch() के अंदर भेजा गया है
|
| 129 |
+
demo.queue(default_concurrency_limit=5).launch(
|
| 130 |
+
theme=gr.themes.Monochrome(),
|
| 131 |
+
css=custom_css
|
| 132 |
+
)
|