Update app.py
Browse files
app.py
CHANGED
|
@@ -1,81 +1,27 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
import
|
| 4 |
|
| 5 |
# ============================================================
|
| 6 |
-
# ১. মডেল
|
| 7 |
# ============================================================
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
system_message,
|
| 14 |
-
max_tokens,
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
hf_token: gr.OAuthToken | None,
|
| 18 |
-
):
|
| 19 |
-
"""
|
| 20 |
-
Hugging Face Inference API - টোকেন অটো-ডিটেক্ট:
|
| 21 |
-
- ইউজার লগইন করলে OAuth Token ব্যবহার করবে
|
| 22 |
-
- না করলে Space Secret-এর HF_TOKEN ব্যবহার করবে
|
| 23 |
-
"""
|
| 24 |
-
# টোকেন নির্বাচন
|
| 25 |
-
if hf_token is not None:
|
| 26 |
-
token = hf_token.token
|
| 27 |
-
else:
|
| 28 |
-
token = os.getenv("HF_TOKEN")
|
| 29 |
-
if token is None:
|
| 30 |
-
yield "⚠️ Please login or set HF_TOKEN in Space Secrets."
|
| 31 |
-
return
|
| 32 |
-
|
| 33 |
-
client = InferenceClient(token=token, model=model_id)
|
| 34 |
-
|
| 35 |
-
messages = [{"role": "system", "content": system_message}]
|
| 36 |
-
messages.extend(history)
|
| 37 |
-
messages.append({"role": "user", "content": message})
|
| 38 |
-
|
| 39 |
-
response = ""
|
| 40 |
-
try:
|
| 41 |
-
for msg in client.chat_completion(
|
| 42 |
-
messages,
|
| 43 |
-
max_tokens=max_tokens,
|
| 44 |
-
stream=True,
|
| 45 |
-
temperature=temperature,
|
| 46 |
-
top_p=top_p,
|
| 47 |
-
):
|
| 48 |
-
token_text = msg.choices[0].delta.content or ""
|
| 49 |
-
response += token_text
|
| 50 |
-
yield response
|
| 51 |
-
except Exception as e:
|
| 52 |
-
yield f"❌ Error: {str(e)}. Please check logs."
|
| 53 |
-
|
| 54 |
-
# ============================================================
|
| 55 |
-
# ২. ChatInterface UI
|
| 56 |
-
# ============================================================
|
| 57 |
-
chatbot = gr.ChatInterface(
|
| 58 |
-
respond,
|
| 59 |
-
additional_inputs=[
|
| 60 |
-
gr.Textbox(
|
| 61 |
-
value="You are BLM, a helpful AI assistant created by MD Mushfiqur Rahim.",
|
| 62 |
-
label="System message"
|
| 63 |
-
),
|
| 64 |
-
gr.Slider(minimum=1, maximum=4096, value=512, step=1, label="Max new tokens"),
|
| 65 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 66 |
-
gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
|
| 67 |
-
],
|
| 68 |
-
title="BLM AI Assistant",
|
| 69 |
-
description="Created by MD Mushfiqur Rahim. Please login (top-left) for full access.",
|
| 70 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
# ============================================================
|
| 73 |
-
#
|
| 74 |
# ============================================================
|
| 75 |
-
|
| 76 |
-
with gr.Sidebar():
|
| 77 |
-
gr.LoginButton()
|
| 78 |
-
chatbot.render()
|
| 79 |
-
|
| 80 |
-
if __name__ == "__main__":
|
| 81 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
+
import torch
|
| 4 |
|
| 5 |
# ============================================================
|
| 6 |
+
# ১. মডেল লোড (আপনার mx-llms/BLM)
|
| 7 |
# ============================================================
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
+
"mx-llms/BLM",
|
| 10 |
+
trust_remote_code=True,
|
| 11 |
+
device_map="auto",
|
| 12 |
+
torch_dtype=torch.float16,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
)
|
| 14 |
+
tokenizer = AutoTokenizer.from_pretrained("mx-llms/BLM", trust_remote_code=True)
|
| 15 |
+
|
| 16 |
+
def respond(message, history):
|
| 17 |
+
messages = [{"role": "user", "content": message}]
|
| 18 |
+
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 19 |
+
inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
|
| 20 |
+
with torch.no_grad():
|
| 21 |
+
outputs = model.generate(**inputs, max_new_tokens=100)
|
| 22 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 23 |
|
| 24 |
# ============================================================
|
| 25 |
+
# ২. Gradio UI
|
| 26 |
# ============================================================
|
| 27 |
+
gr.ChatInterface(fn=respond, title="BLM AI Assistant").launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|