Update app.py
Browse files
app.py
CHANGED
|
@@ -1,72 +1,56 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import torch
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
| 7 |
-
# =========================
|
| 8 |
-
MODEL_NAME = "Qwen/Qwen1.5-1.8B-Chat" # ya jo tum use kar rahe ho
|
| 9 |
|
| 10 |
-
print("
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
model = AutoModelForCausalLM.from_pretrained(
|
| 14 |
-
|
| 15 |
-
torch_dtype=torch.
|
| 16 |
-
device_map="
|
|
|
|
| 17 |
)
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
#
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
inputs = tokenizer(conversation, return_tensors="pt").to(model.device)
|
| 36 |
-
|
| 37 |
outputs = model.generate(
|
| 38 |
**inputs,
|
| 39 |
-
max_new_tokens=
|
| 40 |
temperature=0.7,
|
| 41 |
-
top_p=0.9,
|
| 42 |
do_sample=True
|
| 43 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
response = response.split("Assistant:")[-1].strip()
|
| 49 |
-
|
| 50 |
-
history.append((user_message, response))
|
| 51 |
-
|
| 52 |
-
return history, history
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
# =========================
|
| 56 |
-
# UI
|
| 57 |
-
# =========================
|
| 58 |
-
with gr.Blocks() as demo:
|
| 59 |
-
gr.Markdown("# 🤖 Qwen Chatbot")
|
| 60 |
-
|
| 61 |
-
chatbot = gr.Chatbot() # ✅ FIXED (no type="messages")
|
| 62 |
-
|
| 63 |
-
msg = gr.Textbox(placeholder="Type your message here...")
|
| 64 |
-
clear = gr.Button("Clear")
|
| 65 |
-
|
| 66 |
-
msg.submit(chat, [msg, chatbot], [chatbot, chatbot])
|
| 67 |
-
clear.click(lambda: None, None, chatbot, queue=False)
|
| 68 |
-
|
| 69 |
-
# =========================
|
| 70 |
-
# LAUNCH
|
| 71 |
-
# =========================
|
| 72 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import torch
|
| 3 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
|
| 5 |
+
# 1. Lightweight Model (फ्री CPU सर्वर के लिए एकदम परफेक्ट)
|
| 6 |
+
REPO_ID = "Qwen/Qwen2.5-0.5B-Instruct"
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
print("लोड हो रहा है... कृपया प्रतीक्षा करें...")
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(REPO_ID)
|
| 10 |
|
| 11 |
+
# torch.bfloat16 मेमोरी को आधा कर देता है जिससे क्रैश नहीं होता
|
| 12 |
model = AutoModelForCausalLM.from_pretrained(
|
| 13 |
+
REPO_ID,
|
| 14 |
+
torch_dtype=torch.bfloat16,
|
| 15 |
+
device_map="cpu",
|
| 16 |
+
low_cpu_mem_usage=True
|
| 17 |
)
|
| 18 |
+
print("मॉडल सफलतापूर्वक लोड हो गया!")
|
| 19 |
+
|
| 20 |
+
# 2. Simple Core Logic (सिर्फ रिस्पॉन्स जनरेट करने के लिए)
|
| 21 |
+
def generate_response(message, history):
|
| 22 |
+
# Gradio की हिस्ट्री को Qwen के फॉर्मेट में बदलना
|
| 23 |
+
messages = []
|
| 24 |
+
for user_msg, bot_msg in history:
|
| 25 |
+
messages.append({"role": "user", "content": user_msg})
|
| 26 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 27 |
+
|
| 28 |
+
# नया मैसेज जोड़ना
|
| 29 |
+
messages.append({"role": "user", "content": message})
|
| 30 |
+
|
| 31 |
+
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 32 |
+
inputs = tokenizer([text], return_tensors="pt")
|
| 33 |
+
|
| 34 |
+
# Fast Inference सेटिंग्स
|
|
|
|
|
|
|
| 35 |
outputs = model.generate(
|
| 36 |
**inputs,
|
| 37 |
+
max_new_tokens=150, # जवाब की लिमिट ताकि सर्वर न अटके
|
| 38 |
temperature=0.7,
|
|
|
|
| 39 |
do_sample=True
|
| 40 |
)
|
| 41 |
+
|
| 42 |
+
response_ids = outputs[0][inputs.input_ids.shape[1]:]
|
| 43 |
+
response = tokenizer.decode(response_ids, skip_special_tokens=True)
|
| 44 |
+
|
| 45 |
+
return response
|
| 46 |
+
|
| 47 |
+
# 3. Minimal UI & API Setup (ChatInterface सब खुद हैंडल करेगा)
|
| 48 |
+
demo = gr.ChatInterface(
|
| 49 |
+
fn=generate_response,
|
| 50 |
+
title="⚡ Qwen 0.5B Fast API",
|
| 51 |
+
description="Minimal, Fast & Stable Backend for API Calls",
|
| 52 |
+
)
|
| 53 |
|
| 54 |
+
# show_error=True से हमें असली एरर दिखेगा
|
| 55 |
+
if __name__ == "__main__":
|
| 56 |
+
demo.launch(show_error=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|