Update app.py
Browse files
app.py
CHANGED
|
@@ -1,27 +1,79 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
| 3 |
-
import
|
| 4 |
|
| 5 |
# ============================================================
|
| 6 |
-
# ১. মডেল
|
| 7 |
# ============================================================
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
|
| 17 |
-
messages
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
# ============================================================
|
| 25 |
# ২. Gradio UI
|
| 26 |
# ============================================================
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|