Vedika35 commited on
Commit
a43554c
·
verified ·
1 Parent(s): dfa4338

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +101 -69
app.py CHANGED
@@ -1,91 +1,123 @@
1
  import gradio as gr
2
- from gradio_client import Client
 
 
3
  import os
4
- import time
5
 
6
- # श्रीमान, यहाँ हम आपके द्वारा निर्ित 'Vedika35/Vedika_coder' से जुड़ रहे हैं।
7
- # पलक झपकतिणाम देके लिए हम 'submit' (Streaming) का उपय करेंगे।
8
 
9
- MODAL_NAME = "Vedika35/Vedika_coder"
10
 
11
- def vedika_omni_stream(text, files, system_prompt):
 
 
 
 
 
 
 
 
 
 
12
  """
13
- वेदिका का मुख्जो टे्स्ट और मल्टमीडिया कोिजलीी गि से प्रोसेस करता है।
14
  """
15
- try:
16
- # ्रेडिाइंट सेटअप
17
- client = Client(MODAL_NAME)
18
-
19
- # १. सिस्टम प्रॉम्प्ट को तुरंत लागू करना (apply_prompt)
20
- client.predict(input_value=system_prompt, api_name="/apply_prompt")
 
 
 
21
 
22
- # २. आपे द्वारा िर्धरित 'नन' (None) पेलोड लॉिक
23
- # यदि टेक्स्ट नहीं है तो None, यदि फाइल नहीं है तो None
24
- final_text = text if text and text.strip() != "" else None
25
- final_files = files if files else None
 
 
26
 
27
- # ३. आउपुट नरेकरना (Streaming logic for speed)
28
- # हम यहाँ 'submit' का उपयोग कर रहे हैं जो एक-एक शब्द को 'स्ट्रीम' करेगा
29
- job = client.submit(
30
- input_value={"files": final_files, "text": final_text},
31
- api_name="/generation_code"
32
- )
 
 
 
 
 
33
 
34
- # ४. परिणाम तुरंत यूआई पर भ
35
- for update in job:
36
- # अपडेट डेटा को सुरक्षित रूप से निकालना
37
- if isinstance(update, str):
38
- yield update
39
- elif isinstance(update, list) and len(update) > 0:
40
- # यदि हिस्ट्री या लिस्ट प्राप्त होती है
41
- last_msg = update[-1]
42
- if isinstance(last_msg, dict) and 'content' in last_msg:
43
- yield last_msg['content']
44
- else:
45
- yield str(update[0])
46
- else:
47
- yield str(update)
48
 
49
- except Exception as e:
50
- yield f"⚠️ सिस्टम में त्रुटि: {str(e)}"
 
 
 
51
 
52
- # प्रीमियम डार्क यूआई का निर्माण (प्रीमियम कोडिंग थीम)
53
  custom_css = """
54
- footer {visibility: hidden}
55
- .gradio-container {background-color: #050505 !important; color: #E0E0E0 !important;}
56
- #title-header h1 {background: linear-gradient(90deg, #fff, #3b82f6); -webkit-background-clip: text; -webkit-text-fill-color: transparent;}
 
 
 
 
 
 
 
57
  """
58
 
59
  with gr.Blocks(theme=gr.themes.Monochrome(), css=custom_css) as demo:
60
- with gr.Row(elem_id="title-header"):
61
- gr.Markdown("# 🔱 Vedika 3.5 Coder Engine")
 
 
 
 
 
 
 
 
 
 
62
 
63
  with gr.Row():
64
- with gr.Column(scale=1):
65
- sys_input = gr.Textbox(
66
- label="System Prompt Configuration",
67
- value="You are Vedika 3.5, an elite coding AI assistant. Identify only as Vedika. Provide robust, production-ready code in Markdown.",
68
- lines=3
69
- )
70
- file_input = gr.File(label="Attach Media (None by default)", file_count="multiple")
71
-
72
- with gr.Column(scale=2):
73
- chat_output = gr.Markdown(label="Vedika Core Output")
74
- user_input = gr.Textbox(label="User Query", placeholder="वेदिका के लिए कोडिंग कार्य यहाँ लिखें...", lines=4)
75
-
76
- with gr.Row():
77
- submit_btn = gr.Button("⚡ Execute (Palak Jhapakte)", variant="primary")
78
- clear_btn = gr.Button("Clear Memory")
79
 
80
- # इवेंट्स को जोड़ना
81
- submit_btn.click(
82
- fn=vedika_omni_stream,
83
- inputs=[user_input, file_input, sys_input],
84
- outputs=chat_output
 
 
 
 
 
 
 
 
 
 
 
85
  )
86
-
87
- clear_btn.click(lambda: (None, None, ""), None, [chat_output, file_input, user_input])
88
 
89
  if __name__ == "__main__":
90
- # हगिंग फेस स्पेस के लिए लॉन्च
91
- demo.launch()
 
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
+ # आदरणीय श्रीमान, यहाँ हम आपके मॉडल 'Vedika35/Vedika_coder' को लोड हे हैं।
8
+ # हगिंग फ्ी रैको देखतहुए हम इसे 4-bit में करेंगे ताकि यह 'पलक झपकते ही' चले
9
 
10
+ MODEL_ID = "Vedika35/Vedika_coder"
11
 
12
+ # १. टोकनाइज़र और मॉडल लोडिंग (Loading into Memory)
13
+ print("Loading model into memory, please wait Shriman...")
14
+ tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
15
+ model = AutoModelForCausalLM.from_pretrained(
16
+ MODEL_ID,
17
+ torch_dtype=torch.float16,
18
+ device_map="auto",
19
+ load_in_4bit=True # फ्री स्पेस के लिए ऑप्टिमाइजेशन
20
+ )
21
+
22
+ def vedika_generate(message, history, system_prompt):
23
  """
24
+ क्शआपॉड याददाश्त (Memory) और कोिंग्षम ंचालित करता है।
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
+ # ३. स््रीमिंग ेटअप (For lighting fast response)
44
+ streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
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.7,
52
+ top_p=0.9,
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: #1a1a1a !important; border: 1px solid #333 !important; }
69
+ .message.bot { background-color: transparent !important; }
70
+ footer { visibility: hidden; }
71
+ #header-brand h1 {
72
+ background: linear-gradient(90deg, #FFFFFF, #3b82f6);
73
+ -webkit-background-clip: text;
74
+ -webkit-text-fill-color: transparent;
75
+ font-weight: 800;
76
+ }
77
  """
78
 
79
  with gr.Blocks(theme=gr.themes.Monochrome(), css=custom_css) as demo:
80
+ with gr.Row(elem_id="header-brand"):
81
+ gr.Markdown("# 🔱 Vedika 3.5 Coder")
82
+ gr.Markdown("### भारत के गौरवशाली कोडर के लिए समर्पित | Created by Divy Patel")
83
+
84
+ sys_input = gr.Textbox(
85
+ label="System Instruction",
86
+ value="You are Vedika 3.5, an expert coding assistant created by Divy Patel. Identify only as Vedika. Provide direct, clean code in Markdown.",
87
+ lines=2
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="Input Command",
96
+ placeholder="वेदिका के लिए कोडिंग कार्य यहाँ लिखें...",
97
+ scale=9
98
+ )
99
+ submit_btn = gr.Button("⚡ Execute", scale=1, variant="primary")
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
+ # इवेंट्स को जोड़ना (Palak Jhapakte Logic)
115
+ msg_input.submit(user_msg, [msg_input, chatbot], [msg_input, chatbot], queue=False).then(
116
+ bot_res, [chatbot, sys_input], chatbot
117
+ )
118
+ submit_btn.click(user_msg, [msg_input, chatbot], [msg_input, chatbot], queue=False).then(
119
+ bot_res, [chatbot, sys_input], chatbot
120
  )
 
 
121
 
122
  if __name__ == "__main__":
123
+ demo.queue().launch()