Patel Traders commited on
Commit
c99999b
·
verified ·
1 Parent(s): 86c0842

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -0
app.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # --- 🔱 हनुमान AI: अजेय एवं आत्मनिर्भर मल्टी-फंक्शनल पोर्टल (ZERO CHARGES) ---
2
+ # मार्गदर्शक: दिव्य पटेल जी | भारत 🇮🇳
3
+ # तकनीक: Local CPU Execution (No API Calls = No Charges)
4
+ # मॉडल: Qwen2-VL-2B-Instruct (Reasoning + Vision + File Reading)
5
+
6
+ import gradio as gr
7
+ import torch
8
+ from transformers import Qwen2VLForConditionalGeneration, AutoTokenizer, AutoProcessor
9
+ from qwen_vl_utils import process_vision_info
10
+ import os
11
+
12
+ # दिव्य जी, यहाँ हम मॉडल को स्थानीय रूप से (Locally) लोड कर रहे हैं।
13
+ # यह तरीका 100% मुफ्त है क्योंकि यह आपके स्पेस की रैम (RAM) का उपयोग करता है।
14
+ MODEL_ID = "Qwen/Qwen2-VL-2B-Instruct"
15
+
16
+ print("🔱 हनुमान AI जागृत हो रहा है... सीपीयू पर आत्मनिर्भर लोडिंग जारी है।")
17
+
18
+ # मॉडल और प्रोसेसर को लोड करना (CPU के लिए पूरी तरह ऑप्टिमाइज्ड)
19
+ # हम 'torch_dtype=torch.float32' का उपयोग कर रहे हैं ताकि CPU पर सटीकता बनी रहे
20
+ model = Qwen2VLForConditionalGeneration.from_pretrained(
21
+ MODEL_ID,
22
+ torch_dtype=torch.float32,
23
+ device_map="cpu",
24
+ low_cpu_mem_usage=True
25
+ )
26
+ processor = AutoProcessor.from_pretrained(MODEL_ID)
27
+
28
+ def hanuman_thinking_logic(message, history, file_upload):
29
+ try:
30
+ content = []
31
+
32
+ # १. फाइल या चित्र का विश्लेषण (Multimodal Logic)
33
+ if file_upload is not None:
34
+ file_ext = file_upload.name.split('.')[-1].lower()
35
+ # यदि चित्र है
36
+ if file_ext in ['png', 'jpg', 'jpeg', 'webp']:
37
+ content.append({"type": "image", "image": file_upload.name})
38
+ # यदि टेक्स्ट फाइल है
39
+ else:
40
+ try:
41
+ with open(file_upload.name, 'r', encoding='utf-8') as f:
42
+ file_content = f.read()
43
+ message = f"Document Content:\n{file_content}\n\nUser Question: {message}"
44
+ except:
45
+ return "क्षमा करें दिव्य जी, यह फाइल पढ़ी नहीं जा सकी। केवल टेक्स्ट या चित्र भेजें।"
46
+
47
+ content.append({"type": "text", "text": message})
48
+
49
+ # २. बातचीत का खाका (Template)
50
+ messages = [{"role": "user", "content": content}]
51
+
52
+ # ३. प्रोसेसिंग और जनरेशन
53
+ text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
54
+ image_inputs, video_inputs = process_vision_info(messages)
55
+
56
+ inputs = processor(
57
+ text=[text],
58
+ images=image_inputs,
59
+ videos=video_inputs,
60
+ padding=True,
61
+ return_tensors="pt",
62
+ ).to("cpu")
63
+
64
+ # ४. ८१९२ टोकन की क्षमता के साथ उत्तर का निर्माण
65
+ # 'max_new_tokens' को आप अपनी आवश्यकतानुसार बढ़ा सकते हैं
66
+ generated_ids = model.generate(**inputs, max_new_tokens=1024)
67
+ generated_ids_trimmed = [
68
+ out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
69
+ ]
70
+ response = processor.batch_decode(
71
+ generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
72
+ )[0]
73
+
74
+ return response
75
+
76
+ except Exception as e:
77
+ return f"त्रुटि: {str(e)}। दिव्य जी, कृपया सुनिश्चित करें कि रैम (RAM) खाली है।"
78
+
79
+ # --- 🔱 भव्य इंटरफ़ेस ---
80
+ with gr.Blocks(theme=gr.themes.Soft(primary_hue="orange", secondary_hue="amber")) as demo:
81
+ gr.HTML("<div style='text-align: center;'><h1>🔱 हनुमान AI - आत्मनिर्भर थिंकिंग पोर्टल </h1><p>Pioneered by Divy Patel | 100% Free | No API Costs</p></div>")
82
+
83
+ with gr.Row():
84
+ with gr.Column(scale=4):
85
+ chatbot = gr.Chatbot(height=550)
86
+ with gr.Row():
87
+ msg_input = gr.Textbox(placeholder="अपना जटिल प्रश्न यहाँ लिखें...", scale=7)
88
+ file_input = gr.File(label="फाइल/फोटो अपलोड", scale=2)
89
+ submit_btn = gr.Button("पूछें", variant="primary", scale=1)
90
+
91
+ def respond(message, history, file):
92
+ # थिंकिंग इंजन को कॉल करना
93
+ bot_message = hanuman_thinking_logic(message, history, file)
94
+ history.append((message, bot_message))
95
+ return "", history, None
96
+
97
+ submit_btn.click(respond, [msg_input, chatbot, file_input], [msg_input, chatbot, file_input])
98
+
99
+ gr.Markdown("---")
100
+ gr.Markdown("### दिव्य पटेल जी, यह मॉडल सीधे आपके CPU पर लोड है। इसमें कोई बाहरी API चार्ज नहीं लग सकता।")
101
+
102
+ if __name__ == "__main__":
103
+ demo.launch()