mahin1234 commited on
Commit
a7ccc0f
·
verified ·
1 Parent(s): abd2425

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -16
app.py CHANGED
@@ -3,9 +3,9 @@ from huggingface_hub import InferenceClient
3
  import os
4
 
5
  # ============================================================
6
- # ১. মডেল আইডি (সিক্রেট থেকে অথবা ডিফল্ট)
7
  # ============================================================
8
- model_id = os.getenv("MODEL_ID", "mx-llms/BLM") # আপনার মডেল আইডি
9
 
10
  def respond(
11
  message,
@@ -14,28 +14,42 @@ def respond(
14
  max_tokens,
15
  temperature,
16
  top_p,
17
- hf_token: gr.OAuthToken,
18
  ):
19
  """
20
- Hugging Face Inference API ব্যবহারস্্রিমিং রসপনস।
 
 
21
  """
22
- client = InferenceClient(token=hf_token.token, model=model_id)
 
 
 
 
 
 
 
 
 
23
 
24
  messages = [{"role": "system", "content": system_message}]
25
  messages.extend(history)
26
  messages.append({"role": "user", "content": message})
27
 
28
  response = ""
29
- for message in client.chat_completion(
30
- messages,
31
- max_tokens=max_tokens,
32
- stream=True,
33
- temperature=temperature,
34
- top_p=top_p,
35
- ):
36
- token = message.choices[0].delta.content or ""
37
- response += token
38
- yield response
 
 
 
39
 
40
  # ============================================================
41
  # ২. ChatInterface UI
@@ -52,7 +66,7 @@ chatbot = gr.ChatInterface(
52
  gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
53
  ],
54
  title="BLM AI Assistant",
55
- description="Created by MD Mushfiqur Rahim. Powered by Hugging Face Inference API.",
56
  )
57
 
58
  # ============================================================
 
3
  import os
4
 
5
  # ============================================================
6
+ # ১. মডেল আইডি (সিক্রেট বা ডিফল্ট)
7
  # ============================================================
8
+ model_id = os.getenv("MODEL_ID", "mx-llms/BLM")
9
 
10
  def respond(
11
  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
 
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
  # ============================================================