Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,23 +1,13 @@
|
|
| 1 |
import os
|
| 2 |
|
| 3 |
-
# ১. requirements.txt আপডেট করা (Inference
|
| 4 |
with open("requirements.txt", "w") as f:
|
| 5 |
f.write("gradio\nhuggingface_hub\n")
|
| 6 |
|
| 7 |
-
# ২. app.py আপডেট করা (
|
| 8 |
app_code = """
|
| 9 |
import gradio as gr
|
| 10 |
from huggingface_hub import InferenceClient
|
| 11 |
-
import os
|
| 12 |
-
|
| 13 |
-
# Space Settings-এর Secrets থেকে টোকেনটি নেবে
|
| 14 |
-
hf_token = os.getenv('HF_TOKEN')
|
| 15 |
-
|
| 16 |
-
# মডেল আইডি
|
| 17 |
-
model_id = 'mx-llms/BLM'
|
| 18 |
-
|
| 19 |
-
# Client ইনিশিয়ালাইজ করা (এটি আপনার টোকেন ব্যবহার করে কাজ করবে)
|
| 20 |
-
client = InferenceClient(token=hf_token, model=model_id)
|
| 21 |
|
| 22 |
def respond(
|
| 23 |
message,
|
|
@@ -26,10 +16,15 @@ def respond(
|
|
| 26 |
max_tokens,
|
| 27 |
temperature,
|
| 28 |
top_p,
|
|
|
|
| 29 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
messages = [{"role": "system", "content": system_message}]
|
| 31 |
|
| 32 |
-
#
|
| 33 |
for h in history:
|
| 34 |
messages.append(h)
|
| 35 |
|
|
@@ -37,7 +32,7 @@ def respond(
|
|
| 37 |
|
| 38 |
response = ""
|
| 39 |
|
| 40 |
-
#
|
| 41 |
for message in client.chat_completion(
|
| 42 |
messages,
|
| 43 |
max_tokens=max_tokens,
|
|
@@ -50,8 +45,8 @@ def respond(
|
|
| 50 |
response += token
|
| 51 |
yield response
|
| 52 |
|
| 53 |
-
# Gradio
|
| 54 |
-
|
| 55 |
respond,
|
| 56 |
additional_inputs=[
|
| 57 |
gr.Textbox(value="You are BLM. Created by MD Mushfiqur Rahim.", label="System message"),
|
|
@@ -59,11 +54,15 @@ demo = gr.ChatInterface(
|
|
| 59 |
gr.Slider(minimum=0.1, maximum=2.0, value=0.1, step=0.1, label="Temperature"),
|
| 60 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.9, step=0.05, label="Top-p"),
|
| 61 |
],
|
| 62 |
-
type="messages"
|
| 63 |
-
title="BLM AI Assistant",
|
| 64 |
-
description="Created by MD Mushfiqur Rahim. This assistant is ready to help everyone!"
|
| 65 |
)
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
if __name__ == '__main__':
|
| 68 |
demo.launch()
|
| 69 |
"""
|
|
@@ -71,4 +70,4 @@ if __name__ == '__main__':
|
|
| 71 |
with open("app.py", "w") as f:
|
| 72 |
f.write(app_code)
|
| 73 |
|
| 74 |
-
print("✅
|
|
|
|
| 1 |
import os
|
| 2 |
|
| 3 |
+
# ১. requirements.txt আপডেট করা (Inference API এর জন্য)
|
| 4 |
with open("requirements.txt", "w") as f:
|
| 5 |
f.write("gradio\nhuggingface_hub\n")
|
| 6 |
|
| 7 |
+
# ২. app.py আপডেট করা (InferenceClient ব্যবহার করে)
|
| 8 |
app_code = """
|
| 9 |
import gradio as gr
|
| 10 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
def respond(
|
| 13 |
message,
|
|
|
|
| 16 |
max_tokens,
|
| 17 |
temperature,
|
| 18 |
top_p,
|
| 19 |
+
hf_token: gr.OAuthToken,
|
| 20 |
):
|
| 21 |
+
# Initialize the Inference Client with the provided OAuth token
|
| 22 |
+
# Model: mx-llms/BLM
|
| 23 |
+
client = InferenceClient(token=hf_token.token, model='mx-llms/BLM')
|
| 24 |
+
|
| 25 |
messages = [{"role": "system", "content": system_message}]
|
| 26 |
|
| 27 |
+
# Add history to messages
|
| 28 |
for h in history:
|
| 29 |
messages.append(h)
|
| 30 |
|
|
|
|
| 32 |
|
| 33 |
response = ""
|
| 34 |
|
| 35 |
+
# Request streaming completion
|
| 36 |
for message in client.chat_completion(
|
| 37 |
messages,
|
| 38 |
max_tokens=max_tokens,
|
|
|
|
| 45 |
response += token
|
| 46 |
yield response
|
| 47 |
|
| 48 |
+
# Gradio Interface
|
| 49 |
+
chatbot = gr.ChatInterface(
|
| 50 |
respond,
|
| 51 |
additional_inputs=[
|
| 52 |
gr.Textbox(value="You are BLM. Created by MD Mushfiqur Rahim.", label="System message"),
|
|
|
|
| 54 |
gr.Slider(minimum=0.1, maximum=2.0, value=0.1, step=0.1, label="Temperature"),
|
| 55 |
gr.Slider(minimum=0.1, maximum=1.0, value=0.9, step=0.05, label="Top-p"),
|
| 56 |
],
|
| 57 |
+
type="messages"
|
|
|
|
|
|
|
| 58 |
)
|
| 59 |
|
| 60 |
+
with gr.Blocks() as demo:
|
| 61 |
+
with gr.Sidebar():
|
| 62 |
+
gr.Markdown("### BLM Assistant Login")
|
| 63 |
+
gr.LoginButton()
|
| 64 |
+
chatbot.render()
|
| 65 |
+
|
| 66 |
if __name__ == '__main__':
|
| 67 |
demo.launch()
|
| 68 |
"""
|
|
|
|
| 70 |
with open("app.py", "w") as f:
|
| 71 |
f.write(app_code)
|
| 72 |
|
| 73 |
+
print("✅ app.py has been updated to use Inference API and OAuth!")
|