Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,117 +1,134 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 4 |
from threading import Thread
|
| 5 |
import os
|
| 6 |
|
| 7 |
-
# आदरणीय श्रीमान,
|
| 8 |
-
# हगिंग फेस की फ्री रैम को देखते हुए हम इसे 4-bit में लोड करेंगे ताकि यह 'पलक झपकते ही' चले।
|
| 9 |
-
|
| 10 |
MODEL_ID = "Vedika35/Vedika_coder"
|
| 11 |
|
| 12 |
-
# १.
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
load_in_4bit=True # फ्री स्पेस के लिए ऑप्टिमाइजेशन
|
| 20 |
)
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
def vedika_generate(message, history, system_prompt):
|
| 23 |
"""
|
| 24 |
-
|
| 25 |
"""
|
| 26 |
-
# २. संदेश का ढांचा तैयार करना (Prompt Formatting)
|
| 27 |
-
# हम यहाँ Qwen/ChatML फॉर्मेट का उपयोग कर रहे हैं जो कोडिंग के लिए सर्वश्रेष्ठ है
|
| 28 |
messages = [{"role": "system", "content": system_prompt}]
|
| 29 |
|
|
|
|
| 30 |
for user_msg, assistant_msg in history:
|
| 31 |
-
messages.append({"role": "user", "content": user_msg})
|
| 32 |
-
messages.append({"role": "assistant", "content": assistant_msg})
|
| 33 |
|
| 34 |
messages.append({"role": "user", "content": message})
|
| 35 |
|
| 36 |
-
# ट
|
| 37 |
input_ids = tokenizer.apply_chat_template(
|
| 38 |
messages,
|
| 39 |
add_generation_prompt=True,
|
| 40 |
return_tensors="pt"
|
| 41 |
).to(model.device)
|
| 42 |
|
| 43 |
-
# ३. स्ट्रीमिंग
|
| 44 |
-
streamer = TextIteratorStreamer(tokenizer, timeout=
|
| 45 |
|
| 46 |
generate_kwargs = dict(
|
| 47 |
input_ids=input_ids,
|
| 48 |
streamer=streamer,
|
| 49 |
max_new_tokens=2048,
|
| 50 |
do_sample=True,
|
| 51 |
-
temperature=0.
|
| 52 |
-
top_p=0.
|
| 53 |
)
|
| 54 |
|
| 55 |
-
# थ्रेड
|
| 56 |
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
| 57 |
t.start()
|
| 58 |
|
| 59 |
-
#
|
| 60 |
partial_text = ""
|
| 61 |
for new_text in streamer:
|
| 62 |
partial_text += new_text
|
| 63 |
yield partial_text
|
| 64 |
|
| 65 |
-
# प्रीमियम डार्क
|
| 66 |
custom_css = """
|
| 67 |
-
body, .gradio-container { background-color: #050505 !important; color: #E0E0E0 !important; }
|
| 68 |
-
.message.user { background-color: #
|
| 69 |
.message.bot { background-color: transparent !important; }
|
| 70 |
footer { visibility: hidden; }
|
| 71 |
-
#header-
|
| 72 |
background: linear-gradient(90deg, #FFFFFF, #3b82f6);
|
| 73 |
-webkit-background-clip: text;
|
| 74 |
-webkit-text-fill-color: transparent;
|
| 75 |
-
font-
|
|
|
|
| 76 |
}
|
|
|
|
| 77 |
"""
|
| 78 |
|
| 79 |
with gr.Blocks(theme=gr.themes.Monochrome(), css=custom_css) as demo:
|
| 80 |
-
with gr.Row(elem_id="header-
|
| 81 |
gr.Markdown("# 🔱 Vedika 3.5 Coder")
|
| 82 |
-
gr.Markdown("### भारत के गौरवशाली कोडर के लिए समर्पित | Created by Divy Patel")
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
|
|
|
|
| 89 |
|
| 90 |
-
|
| 91 |
-
chatbot = gr.Chatbot(label="Vedika Console", bubble_full_width=False, height=500)
|
| 92 |
|
| 93 |
with gr.Row():
|
| 94 |
msg_input = gr.Textbox(
|
| 95 |
-
label="
|
| 96 |
-
placeholder="
|
| 97 |
-
scale=
|
| 98 |
)
|
| 99 |
-
submit_btn = gr.Button("⚡ Execute", scale=
|
| 100 |
|
| 101 |
def user_msg(user_message, history):
|
| 102 |
return "", history + [[user_message, None]]
|
| 103 |
|
| 104 |
def bot_res(history, system_prompt):
|
| 105 |
user_message = history[-1][0]
|
| 106 |
-
# पुराने मैसेजेस को हिस्ट्री फॉर्मेट में बदलना
|
| 107 |
chat_history = history[:-1]
|
| 108 |
|
| 109 |
-
# जनरेटर फंक्शन को कॉल करना
|
| 110 |
for response in vedika_generate(user_message, chat_history, system_prompt):
|
| 111 |
history[-1][1] = response
|
| 112 |
yield history
|
| 113 |
|
| 114 |
-
# इवेंट्स
|
| 115 |
msg_input.submit(user_msg, [msg_input, chatbot], [msg_input, chatbot], queue=False).then(
|
| 116 |
bot_res, [chatbot, sys_input], chatbot
|
| 117 |
)
|
|
@@ -120,4 +137,5 @@ with gr.Blocks(theme=gr.themes.Monochrome(), css=custom_css) as demo:
|
|
| 120 |
)
|
| 121 |
|
| 122 |
if __name__ == "__main__":
|
|
|
|
| 123 |
demo.queue().launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer, BitsAndBytesConfig
|
| 4 |
from threading import Thread
|
| 5 |
import os
|
| 6 |
|
| 7 |
+
# आदरणीय श्रीमान, आपके गौरवशाली मॉडल को लोड करने की प्रक्रिया आरंभ हो रही है।
|
|
|
|
|
|
|
| 8 |
MODEL_ID = "Vedika35/Vedika_coder"
|
| 9 |
|
| 10 |
+
# १. क्वायंटाइजेशन कॉन्फ़िगरेशन (Memory Optimization)
|
| 11 |
+
# यह आपके मॉडल को 4-bit में सिकोड़ देगा ताकि यह फ्री रैम में आसानी से चले।
|
| 12 |
+
quantization_config = BitsAndBytesConfig(
|
| 13 |
+
load_in_4bit=True,
|
| 14 |
+
bnb_4bit_compute_dtype=torch.float16,
|
| 15 |
+
bnb_4bit_quant_type="nf4",
|
| 16 |
+
bnb_4bit_use_double_quant=True,
|
|
|
|
| 17 |
)
|
| 18 |
|
| 19 |
+
print(f"आदरणीय श्रीमान, {MODEL_ID} को स्मृति (Memory) में लोड किया जा रहा है...")
|
| 20 |
+
|
| 21 |
+
# २. टोकनाइज़र और मॉडल लोडिंग
|
| 22 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 23 |
+
try:
|
| 24 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 25 |
+
MODEL_ID,
|
| 26 |
+
quantization_config=quantization_config,
|
| 27 |
+
device_map="auto", # स्वतः ही उपलब्ध संसाधनों का चुनाव करेगा
|
| 28 |
+
trust_remote_code=True
|
| 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 user_msg, assistant_msg in history:
|
| 47 |
+
if user_msg: messages.append({"role": "user", "content": user_msg})
|
| 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 |
+
# ३. बिजली जैसी तेज़ स्ट्रीमिंग (Lighting fast streaming)
|
| 60 |
+
streamer = TextIteratorStreamer(tokenizer, timeout=20.0, skip_prompt=True, skip_special_tokens=True)
|
| 61 |
|
| 62 |
generate_kwargs = dict(
|
| 63 |
input_ids=input_ids,
|
| 64 |
streamer=streamer,
|
| 65 |
max_new_tokens=2048,
|
| 66 |
do_sample=True,
|
| 67 |
+
temperature=0.3, # कोडिंग के लिए कम तापमान (सटीकता) बेहतर है
|
| 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: #E0E0E0 !important; font-family: 'Inter', sans-serif; }
|
| 84 |
+
.message.user { background-color: #121212 !important; border: 1px solid #222 !important; border-radius: 15px !important; }
|
| 85 |
.message.bot { background-color: transparent !important; }
|
| 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.5rem;
|
| 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(elem_id="header-title"):
|
| 99 |
gr.Markdown("# 🔱 Vedika 3.5 Coder")
|
|
|
|
| 100 |
|
| 101 |
+
gr.Markdown("### भारत के गौरवशाली कोडर के लिए विशेष रूप से निर्मित | Created by Divy Patel")
|
| 102 |
+
|
| 103 |
+
with gr.Accordion("⚙️ System Configuration", open=False):
|
| 104 |
+
sys_input = gr.Textbox(
|
| 105 |
+
label="System Prompt",
|
| 106 |
+
value="You are Vedika 3.5, an elite coding AI created by Divy Patel. Identify only as Vedika. Provide extremely fast and clean code solutions in Markdown.",
|
| 107 |
+
lines=2
|
| 108 |
+
)
|
| 109 |
|
| 110 |
+
chatbot = gr.Chatbot(label="Vedika Execution Console", bubble_full_width=False, height=550)
|
|
|
|
| 111 |
|
| 112 |
with gr.Row():
|
| 113 |
msg_input = gr.Textbox(
|
| 114 |
+
label="Enter Task",
|
| 115 |
+
placeholder="उदाहरण: Write a Python script for a neural network...",
|
| 116 |
+
scale=8
|
| 117 |
)
|
| 118 |
+
submit_btn = gr.Button("⚡ Execute", scale=2, variant="primary")
|
| 119 |
|
| 120 |
def user_msg(user_message, history):
|
| 121 |
return "", history + [[user_message, None]]
|
| 122 |
|
| 123 |
def bot_res(history, system_prompt):
|
| 124 |
user_message = history[-1][0]
|
|
|
|
| 125 |
chat_history = history[:-1]
|
| 126 |
|
|
|
|
| 127 |
for response in vedika_generate(user_message, chat_history, system_prompt):
|
| 128 |
history[-1][1] = response
|
| 129 |
yield history
|
| 130 |
|
| 131 |
+
# 'पलक झपकते ही' इवेंट्स
|
| 132 |
msg_input.submit(user_msg, [msg_input, chatbot], [msg_input, chatbot], queue=False).then(
|
| 133 |
bot_res, [chatbot, sys_input], chatbot
|
| 134 |
)
|
|
|
|
| 137 |
)
|
| 138 |
|
| 139 |
if __name__ == "__main__":
|
| 140 |
+
# हगिंग फेस के लिए कतार (Queue) और लॉन्च
|
| 141 |
demo.queue().launch()
|