MD Musfiqure Rahim commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,69 +1,164 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
message,
|
| 7 |
-
history: list[dict[str, str]],
|
| 8 |
-
system_message,
|
| 9 |
-
max_tokens,
|
| 10 |
-
temperature,
|
| 11 |
-
top_p,
|
| 12 |
-
hf_token: gr.OAuthToken,
|
| 13 |
-
):
|
| 14 |
-
"""
|
| 15 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 16 |
-
"""
|
| 17 |
-
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 18 |
-
|
| 19 |
-
messages = [{"role": "system", "content": system_message}]
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
|
|
|
| 24 |
|
| 25 |
-
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
messages,
|
| 29 |
-
max_tokens=max_tokens,
|
| 30 |
-
stream=True,
|
| 31 |
-
temperature=temperature,
|
| 32 |
-
top_p=top_p,
|
| 33 |
-
):
|
| 34 |
-
choices = message.choices
|
| 35 |
-
token = ""
|
| 36 |
-
if len(choices) and choices[0].delta.content:
|
| 37 |
-
token = choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
"""
|
| 46 |
-
chatbot = gr.ChatInterface(
|
| 47 |
-
respond,
|
| 48 |
-
additional_inputs=[
|
| 49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 50 |
-
gr.Slider(minimum=1, maximum=2048, 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(
|
| 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 |
-
with gr.Blocks() as demo:
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Gradient.com এ Lychee-GPT-9B চালানোর জন্য script
|
| 4 |
+
Gradient Notebook এ এটা run করুন
|
| 5 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
import torch
|
| 8 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 9 |
+
import gradio as gr
|
| 10 |
+
import os
|
| 11 |
|
| 12 |
+
print("🚀 Installing dependencies...")
|
| 13 |
+
os.system("pip install -q torch transformers gradio accelerate safetensors")
|
| 14 |
|
| 15 |
+
print("📥 Loading Lychee-GPT-9B model...")
|
| 16 |
+
print("⏱️ এটা 5-10 মিনিট লাগতে পারে...")
|
| 17 |
|
| 18 |
+
MODEL_ID = "mx-llms/Lychee-GPT-9B"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
try:
|
| 21 |
+
# Load tokenizer
|
| 22 |
+
tokenizer = AutoTokenizer.from_pretrained(
|
| 23 |
+
MODEL_ID,
|
| 24 |
+
trust_remote_code=True
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
# Load model
|
| 28 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 29 |
+
MODEL_ID,
|
| 30 |
+
torch_dtype=torch.float32,
|
| 31 |
+
device_map="auto", # Gradient automatically optimizes
|
| 32 |
+
trust_remote_code=True,
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
model.eval()
|
| 36 |
+
print("✅ Model loaded successfully!")
|
| 37 |
+
|
| 38 |
+
except Exception as e:
|
| 39 |
+
print(f"❌ Error loading model: {e}")
|
| 40 |
+
model = None
|
| 41 |
+
tokenizer = None
|
| 42 |
|
| 43 |
+
def generate_text(prompt, max_length=256, temperature=0.7, top_p=0.9):
|
| 44 |
+
"""Generate text using Lychee-GPT-9B"""
|
| 45 |
+
if model is None or tokenizer is None:
|
| 46 |
+
return "❌ Model failed to load"
|
| 47 |
+
|
| 48 |
+
try:
|
| 49 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 50 |
+
|
| 51 |
+
with torch.no_grad():
|
| 52 |
+
output = model.generate(
|
| 53 |
+
inputs["input_ids"],
|
| 54 |
+
max_new_tokens=int(max_length),
|
| 55 |
+
temperature=float(temperature),
|
| 56 |
+
top_p=float(top_p),
|
| 57 |
+
do_sample=True,
|
| 58 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
response = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 62 |
+
|
| 63 |
+
if prompt in response:
|
| 64 |
+
response = response.replace(prompt, "", 1).strip()
|
| 65 |
+
|
| 66 |
+
return response if response else "No response generated"
|
| 67 |
+
|
| 68 |
+
except Exception as e:
|
| 69 |
+
return f"❌ Error: {str(e)}"
|
| 70 |
|
| 71 |
+
# Create Gradio interface
|
| 72 |
+
print("\n🎨 Creating Gradio interface...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
with gr.Blocks(title="Lychee-GPT-9B", theme=gr.themes.Soft()) as demo:
|
| 75 |
+
gr.Markdown("""
|
| 76 |
+
# 🎉 Lychee-GPT-9B Demo
|
| 77 |
+
|
| 78 |
+
আপনার নিজস্ব LLM Model - Gradient এ চলছে!
|
| 79 |
+
|
| 80 |
+
⏱️ **প্রথম response একটু slow হতে পারে (model warming up)**
|
| 81 |
+
""")
|
| 82 |
+
|
| 83 |
+
with gr.Row():
|
| 84 |
+
with gr.Column(scale=1):
|
| 85 |
+
prompt = gr.Textbox(
|
| 86 |
+
label="প্রশ্ন/Prompt",
|
| 87 |
+
placeholder="কিছু লিখুন...",
|
| 88 |
+
lines=4,
|
| 89 |
+
info="আপনার প্রশ্ন বা prompt দিন"
|
| 90 |
+
)
|
| 91 |
+
|
| 92 |
+
with gr.Row():
|
| 93 |
+
max_len = gr.Slider(
|
| 94 |
+
label="Max Length",
|
| 95 |
+
minimum=10,
|
| 96 |
+
maximum=512,
|
| 97 |
+
value=256,
|
| 98 |
+
step=10,
|
| 99 |
+
)
|
| 100 |
+
|
| 101 |
+
with gr.Row():
|
| 102 |
+
temp = gr.Slider(
|
| 103 |
+
label="Temperature",
|
| 104 |
+
minimum=0.0,
|
| 105 |
+
maximum=1.0,
|
| 106 |
+
value=0.7,
|
| 107 |
+
step=0.1,
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
+
top_p = gr.Slider(
|
| 111 |
+
label="Top P",
|
| 112 |
+
minimum=0.0,
|
| 113 |
+
maximum=1.0,
|
| 114 |
+
value=0.9,
|
| 115 |
+
step=0.05,
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
submit_btn = gr.Button("🚀 Generate", variant="primary", size="lg")
|
| 119 |
+
|
| 120 |
+
with gr.Column(scale=1):
|
| 121 |
+
output = gr.Textbox(
|
| 122 |
+
label="Response",
|
| 123 |
+
lines=10,
|
| 124 |
+
interactive=False,
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
# Examples
|
| 128 |
+
gr.Examples(
|
| 129 |
+
examples=[
|
| 130 |
+
["বাংলা ভাষা সম্পর্কে বলুন"],
|
| 131 |
+
["পাইথন প্রোগ্রামিং কি?"],
|
| 132 |
+
["একটি সংক্ষিপ্ত গল্প বলুন"],
|
| 133 |
+
["কৃত্রিম বুদ্ধিমত্তা কি?"],
|
| 134 |
+
],
|
| 135 |
+
inputs=prompt,
|
| 136 |
+
label="উদাহরণ প্রশ্ন"
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
# Connect button
|
| 140 |
+
submit_btn.click(
|
| 141 |
+
fn=generate_text,
|
| 142 |
+
inputs=[prompt, max_len, temp, top_p],
|
| 143 |
+
outputs=output,
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
# Allow Enter key
|
| 147 |
+
prompt.submit(
|
| 148 |
+
fn=generate_text,
|
| 149 |
+
inputs=[prompt, max_len, temp, top_p],
|
| 150 |
+
outputs=output,
|
| 151 |
+
)
|
| 152 |
|
| 153 |
+
print("\n🌐 Launching Gradio interface...")
|
| 154 |
+
print("=" * 50)
|
| 155 |
+
print("Space URL will appear below 👇")
|
| 156 |
+
print("=" * 50)
|
| 157 |
|
| 158 |
+
# Launch
|
| 159 |
+
demo.launch(
|
| 160 |
+
server_name="0.0.0.0",
|
| 161 |
+
server_port=7860,
|
| 162 |
+
share=True,
|
| 163 |
+
show_error=True,
|
| 164 |
+
)
|