Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,11 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
def respond(
|
| 6 |
message,
|
|
@@ -12,18 +17,15 @@ def respond(
|
|
| 12 |
hf_token: gr.OAuthToken,
|
| 13 |
):
|
| 14 |
"""
|
| 15 |
-
|
| 16 |
"""
|
| 17 |
-
client = InferenceClient(token=hf_token.token, model=
|
| 18 |
|
| 19 |
messages = [{"role": "system", "content": system_message}]
|
| 20 |
-
|
| 21 |
messages.extend(history)
|
| 22 |
-
|
| 23 |
messages.append({"role": "user", "content": message})
|
| 24 |
|
| 25 |
response = ""
|
| 26 |
-
|
| 27 |
for message in client.chat_completion(
|
| 28 |
messages,
|
| 29 |
max_tokens=max_tokens,
|
|
@@ -31,39 +33,35 @@ def respond(
|
|
| 31 |
temperature=temperature,
|
| 32 |
top_p=top_p,
|
| 33 |
):
|
| 34 |
-
|
| 35 |
-
token = ""
|
| 36 |
-
if len(choices) and choices[0].delta.content:
|
| 37 |
-
token = choices[0].delta.content
|
| 38 |
-
|
| 39 |
response += token
|
| 40 |
yield response
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
"""
|
| 46 |
chatbot = gr.ChatInterface(
|
| 47 |
respond,
|
| 48 |
additional_inputs=[
|
| 49 |
-
gr.Textbox(
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
gr.Slider(
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
),
|
|
|
|
|
|
|
|
|
|
| 59 |
],
|
|
|
|
|
|
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
| 63 |
with gr.Sidebar():
|
| 64 |
gr.LoginButton()
|
| 65 |
chatbot.render()
|
| 66 |
|
| 67 |
-
|
| 68 |
if __name__ == "__main__":
|
| 69 |
-
demo.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", "mx-llms/BLM") # আপনার মডেল আইডি
|
| 9 |
|
| 10 |
def respond(
|
| 11 |
message,
|
|
|
|
| 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,
|
|
|
|
| 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
|
| 42 |
+
# ============================================================
|
|
|
|
| 43 |
chatbot = gr.ChatInterface(
|
| 44 |
respond,
|
| 45 |
additional_inputs=[
|
| 46 |
+
gr.Textbox(
|
| 47 |
+
value="You are BLM, a helpful AI assistant created by MD Mushfiqur Rahim.",
|
| 48 |
+
label="System message"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
),
|
| 50 |
+
gr.Slider(minimum=1, maximum=4096, value=512, step=1, label="Max new tokens"),
|
| 51 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 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 |
+
# ============================================================
|
| 59 |
+
# ৩. Login Button সহ ডেমো
|
| 60 |
+
# ============================================================
|
| 61 |
+
with gr.Blocks(title="BLM AI Assistant") as demo:
|
| 62 |
with gr.Sidebar():
|
| 63 |
gr.LoginButton()
|
| 64 |
chatbot.render()
|
| 65 |
|
|
|
|
| 66 |
if __name__ == "__main__":
|
| 67 |
+
demo.launch()
|