Vedika commited on
Commit
264f23e
·
verified ·
1 Parent(s): 25d6c4d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -30
app.py CHANGED
@@ -1,70 +1,104 @@
1
- # --- 🔱 वेदिका 3.5 फ्लैश: भारत का अपना 2B AI (Super Fast Version) ---
2
  # रचयिता एवं मार्गदर्शक: दिव्य पटेल जी | भारत 🇮🇳
3
- # विशेषता: Ultra-Fast (bfloat16), Memory Safe, No Crash on 2nd Question
4
 
5
  import gradio as gr
6
  import torch
7
- from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
 
8
  from threading import Thread
9
  import os
10
 
11
- print("🔱 भारत का अजेय AI 'वेदिका 3.5 फ्लैश' सुपर-फास्ट में जागृत हो रहा है...")
12
 
13
  # 🛡️ मुफ़्त सर्वर (2 vCPU) के लिए CPU को पूर्णतः अनुकूलित (Optimize) करना
14
  os.environ["OMP_NUM_THREADS"] = "2"
15
  torch.set_num_threads(2)
16
 
17
- # 🚀 दिव्य जी का अपना स्वदेशी 2B मॉडल
18
  MODEL_ID = "pateltraders55455/Vedika-3.5-flash"
19
 
20
  try:
21
- print(f"🔱 '{MODEL_ID}' (2B) लोड किया जा रहा है...")
22
 
23
- tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
 
24
 
25
- # ⚡ ब्रह्मास्त्र 1: bfloat16 का उपयोग करके गति को दोगुना करना
26
  model = AutoModelForCausalLM.from_pretrained(
27
  MODEL_ID,
28
  device_map="cpu",
29
- torch_dtype=torch.bfloat16, # गति बढ़ाने के लिए हल्का डेटा प्रकार
30
- low_cpu_mem_usage=True, # रैम की भारी बचत
31
  trust_remote_code=True
32
  )
33
- print("🔱 विजय! 'वेदिका 3.5 फ्लैश' सुपर-फास गति केाथ सथापि हो गया है!")
34
 
35
  except Exception as e:
36
  print(f"🔱 मॉडल लोडिंग में त्रुटि: {e}")
37
- model, tokenizer = None, None
38
 
39
  def generate_vedika_magic(message, history):
40
- """वेदिका 3.5 फ्लैश का रैश-री और तीव्र इंजन"""
41
 
42
- if model is None or tokenizer is None:
43
  yield "🔱 सिस्टम त्रुटि: मॉडल लोड नहीं हो सका। कृपया अपनी रिपॉजिटरी की जाँच करें।"
44
  return
45
 
46
- # 🛡️ ब्रह्मा्त्र 2: स्मार्मेमो िमिट (एरर से बचािए)
47
- # यह केवल पिछले 2 सवालों को याद रखेगा, ताकि CPU कभी ओवरलोड न हो और क्रैश न करे!
 
 
 
 
 
 
 
48
  recent_history = history[-2:] if len(history) > 2 else history
49
 
50
  messages = [
51
- {"role": "system", "content": "You are 'Vedika 3.5 Flash', an ultra-fast, 2 Billion parameter AI model created entirely by Divy Patel in Bharat (India). Always be highly intelligent, respectful, and proud of your Indian origin."},
52
  ]
53
 
54
  # सीमित इतिहास (History) जोड़ना
55
- for user_msg, ai_msg in recent_history:
56
- messages.append({"role": "user", "content": user_msg})
57
- messages.append({"role": "assistant", "content": ai_msg})
 
 
 
 
58
 
59
- messages.append({"role": "user", "content": message})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
  try:
62
- text_prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
63
- inputs = tokenizer([text_prompt], return_tensors="pt").to(model.device)
 
 
 
 
 
 
64
 
65
- streamer = TextIteratorStreamer(tokenizer, timeout=60.0, skip_prompt=True, skip_special_tokens=True)
66
 
67
- # ⚡ गति बढ़ाने के लिए जनरेशन सेटिंग्स
68
  generate_kwargs = dict(
69
  **inputs,
70
  streamer=streamer,
@@ -72,7 +106,7 @@ def generate_vedika_magic(message, history):
72
  temperature=0.7,
73
  top_p=0.9,
74
  do_sample=True,
75
- use_cache=True # ⚡ ब्रह्मास्त्र 3: कैशिंग से स्पीड बढ़ाना
76
  )
77
 
78
  t = Thread(target=model.generate, kwargs=generate_kwargs)
@@ -87,14 +121,19 @@ def generate_vedika_magic(message, history):
87
  yield f"🔱 प्रसंस्करण त्रुटि: {str(e)}"
88
 
89
  # ============================================================================
90
- # 🔱 वेदिका 3.5 फ्लैश का शुद्ध यूआई (कोई अतिरिक्त आर्गुमेंट नहीं)
91
  # ============================================================================
 
92
 
93
  demo = gr.ChatInterface(
94
  fn=generate_vedika_magic,
95
- title="🔱 Vedika 3.5 Flash (Super Fast)",
96
- description="**Pioneered by Divy Patel | Bharat 🇮🇳**<br>यह भारत का अपना स्वदेशी 2 बिलियन पैरामीटर वाला AI मॉडल है (गति और सुरक्षा के लिए अनुकूलित)",
97
- textbox=gr.Textbox(placeholder="वेदिका 3.5 ्लै सेुछ भी पूछें..."),
 
 
 
 
98
  concurrency_limit=1
99
  )
100
 
 
1
+ # --- 🔱 वेदिका 3.5 फ्लैश: भारत का अपना 2B AI (Multimodal Vision) ---
2
  # रचयिता एवं मार्गदर्शक: दिव्य पटेल जी | भारत 🇮🇳
3
+ # विशेषता: Image Reading (Vision), Ultra-Fast, Memory Safe
4
 
5
  import gradio as gr
6
  import torch
7
+ from transformers import AutoModelForCausalLM, AutoProcessor, TextIteratorStreamer
8
+ from PIL import Image
9
  from threading import Thread
10
  import os
11
 
12
+ print("🔱 भारत का अजेय AI 'वेदिका 3.5 फ्लैश' (मल्टल) जागृत हो रहा है...")
13
 
14
  # 🛡️ मुफ़्त सर्वर (2 vCPU) के लिए CPU को पूर्णतः अनुकूलित (Optimize) करना
15
  os.environ["OMP_NUM_THREADS"] = "2"
16
  torch.set_num_threads(2)
17
 
18
+ # 🚀 दिव्य जी का अपना स्वदेशी 2B मल्टीमॉडल (विज़न) मॉडल
19
  MODEL_ID = "pateltraders55455/Vedika-3.5-flash"
20
 
21
  try:
22
+ print(f"🔱 '{MODEL_ID}' (2B Vision) लोड किया जा रहा है...")
23
 
24
+ # मल्टीमीडिया (चित्र और टेक्स्ट) को एक साथ समझने के लिए AutoProcessor
25
+ processor = AutoProcessor.from_pretrained(MODEL_ID, trust_remote_code=True)
26
 
27
+ # ⚡ bfloat16 का उपयोग करके गति को दोगुना करना
28
  model = AutoModelForCausalLM.from_pretrained(
29
  MODEL_ID,
30
  device_map="cpu",
31
+ torch_dtype=torch.bfloat16, # गति बढ़ाने के लिए
32
+ low_cpu_mem_usage=True, # रैम की बचत
33
  trust_remote_code=True
34
  )
35
+ print("🔱 विजय! 'वेदिका 3.5 फ्लैश' की दिव दृष्टि (Vision)ि हो ग है!")
36
 
37
  except Exception as e:
38
  print(f"🔱 मॉडल लोडिंग में त्रुटि: {e}")
39
+ model, processor = None, None
40
 
41
  def generate_vedika_magic(message, history):
42
+ """वेदिका 3.5 फ्लैश का दृषटि-संपन और तीव्र इंजन"""
43
 
44
+ if model is None or processor is None:
45
  yield "🔱 सिस्टम त्रुटि: मॉडल लोड नहीं हो सका। कृपया अपनी रिपॉजिटरी की जाँच करें।"
46
  return
47
 
48
+ # 1.ंदेश से टेक्स्ट ित्र (Images)िना
49
+ text_input = message.get("text", "")
50
+ files = message.get("files", [])
51
+
52
+ if not text_input.strip() and not files:
53
+ yield "कृपया कुछ प्रश्न पूछें या कोई चित्र अपलोड करें।"
54
+ return
55
+
56
+ # 🛡️ स्मार्ट मेमोरी लिमिट (केवल पिछले 2 सवाल याद रखेगा)
57
  recent_history = history[-2:] if len(history) > 2 else history
58
 
59
  messages = [
60
+ {"role": "system", "content": "You are 'Vedika 3.5 Flash', an ultra-fast, Multimodal 2 Billion parameter AI model created entirely by Divy Patel in Bharat (India). You can analyze images perfectly. Always be highly intelligent, respectful, and proud of your Indian origin."},
61
  ]
62
 
63
  # सीमित इतिहास (History) जोड़ना
64
+ for past_msg in recent_history:
65
+ u_msg, a_msg = past_msg[0], past_msg[1]
66
+ # यदि इतिहास में कोई चित्र (Tuple/Dict) था, तो उसे क्रैश से बचाने के लिए सुरक्षित टेक्स्ट में बदलें
67
+ if isinstance(u_msg, (tuple, dict, list)):
68
+ u_msg = "[Image Uploaded]"
69
+ messages.append({"role": "user", "content": str(u_msg)})
70
+ messages.append({"role": "assistant", "content": str(a_msg)})
71
 
72
+ # 2. चित्र और टेक्स्ट का नया डेटा तैयार करना
73
+ user_content = []
74
+ images_list = []
75
+
76
+ # यदि चित्र अपलोड किए गए हैं
77
+ for file_path in files:
78
+ user_content.append({"type": "image"})
79
+ try:
80
+ images_list.append(Image.open(file_path).convert("RGB"))
81
+ except Exception as e:
82
+ print(f"चित्र पढ़ने में त्रुटि: {e}")
83
+
84
+ # यदि टेक्स्ट लिखा गया है
85
+ if text_input:
86
+ user_content.append({"type": "text", "text": text_input})
87
+
88
+ messages.append({"role": "user", "content": user_content})
89
 
90
  try:
91
+ # 3. मल्टीमॉडल प्रॉम्प्ट तैयार करना
92
+ text_prompt = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
93
+
94
+ # ⚡ प्रोसेसर को चित्र और टेक्स्ट दोनों देना
95
+ if images_list:
96
+ inputs = processor(text=[text_prompt], images=images_list, return_tensors="pt").to(model.device)
97
+ else:
98
+ inputs = processor(text=[text_prompt], return_tensors="pt").to(model.device)
99
 
100
+ streamer = TextIteratorStreamer(processor.tokenizer, timeout=60.0, skip_prompt=True, skip_special_tokens=True)
101
 
 
102
  generate_kwargs = dict(
103
  **inputs,
104
  streamer=streamer,
 
106
  temperature=0.7,
107
  top_p=0.9,
108
  do_sample=True,
109
+ use_cache=True # ⚡ कैशिंग से स्पीड बढ़ाना
110
  )
111
 
112
  t = Thread(target=model.generate, kwargs=generate_kwargs)
 
121
  yield f"🔱 प्रसंस्करण त्रुटि: {str(e)}"
122
 
123
  # ============================================================================
124
+ # 🔱 वेदिका 3.5 फ्लैश का शुद्ध मल्टीमॉडल यूआई
125
  # ============================================================================
126
+ # कोई अतिरिक्त आर्गुमेंट नहीं, केवल शुद्ध मल्टीमॉडल चैट
127
 
128
  demo = gr.ChatInterface(
129
  fn=generate_vedika_magic,
130
+ multimodal=True, # यही ���ह अस्त्र है जो चित्र अपलोड करने का बटन (📎) देगा!
131
+ title="🔱 Vedika 3.5 Flash (Multimodal)",
132
+ description="**Pioneered by Divy Patel | Bharat 🇮🇳**<br>यह भारत का अपना वदेशी 2B म्टीमॉडल AI ह चित्र अपलोड रें और्रश्न पूछें!",
133
+ textbox=gr.MultimodalTextbox(
134
+ placeholder="यहाँ प्रश्न लिखें या चित्र (📎) अपलोड करें...",
135
+ file_types=["image"]
136
+ ),
137
  concurrency_limit=1
138
  )
139