mahin1234 commited on
Commit
b801fd9
·
verified ·
1 Parent(s): 8dbbbb7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +70 -18
app.py CHANGED
@@ -1,27 +1,79 @@
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()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from huggingface_hub import InferenceClient
3
+ import os
4
 
5
  # ============================================================
6
+ # ১. মডেল সেটআপ
7
  # ============================================================
8
+ model_id = os.getenv("MODEL_ID", "mahin1234/lychee-gpt")
9
+
10
+ def respond(
11
+ message,
12
+ history: list[dict[str, str]],
13
+ system_message,
14
+ max_tokens,
15
+ temperature,
16
+ top_p,
17
+ hf_token: gr.OAuthToken | None,
18
+ ):
19
+ """
20
+ Lychee GPT - Hugging Face Inference API ব্যবহার করে স্ট্রিমিং রেসপন্স
21
+ """
22
+ # টোকেন নির্বাচন (লগইন করলে OAuth, নইলে Secret)
23
+ if hf_token is not None:
24
+ token = hf_token.token
25
+ else:
26
+ token = os.getenv("HF_TOKEN")
27
+ if token is None:
28
+ yield "⚠️ Please login or set HF_TOKEN in Space Secrets."
29
+ return
30
+
31
+ client = InferenceClient(token=token, model=model_id)
32
 
33
+ messages = [{"role": "system", "content": system_message}]
34
+ messages.extend(history)
35
+ messages.append({"role": "user", "content": message})
36
+
37
+ response = ""
38
+ try:
39
+ for msg in client.chat_completion(
40
+ messages,
41
+ max_tokens=max_tokens,
42
+ stream=True,
43
+ temperature=temperature,
44
+ top_p=top_p,
45
+ ):
46
+ token_text = msg.choices[0].delta.content or ""
47
+ response += token_text
48
+ yield response
49
+ except Exception as e:
50
+ yield f"❌ Error: {str(e)}. Check logs or model availability."
51
 
52
  # ============================================================
53
  # ২. Gradio UI
54
  # ============================================================
55
+ chatbot = gr.ChatInterface(
56
+ respond,
57
+ additional_inputs=[
58
+ gr.Textbox(
59
+ value="You are Lychee GPT, a helpful AI assistant created by MX LLMS and MD Mushfiqur Rahim.",
60
+ label="System message"
61
+ ),
62
+ gr.Slider(minimum=1, maximum=4096, value=512, step=1, label="Max new tokens"),
63
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
64
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
65
+ ],
66
+ title="🍈 Lychee GPT",
67
+ description="Created by MD Mushfiqur Rahim & MX LLMS. Powered by Hugging Face Inference API.",
68
+ )
69
+
70
+ # ============================================================
71
+ # ৩. ডেমো (Login Button সহ)
72
+ # ============================================================
73
+ with gr.Blocks(title="Lychee GPT") as demo:
74
+ with gr.Sidebar():
75
+ gr.LoginButton()
76
+ chatbot.render()
77
+
78
+ if __name__ == "__main__":
79
+ demo.launch()