Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,104 +1,120 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM, TextStreamer
|
| 3 |
-
from peft import PeftModel
|
| 4 |
-
import torch
|
| 5 |
-
|
| 6 |
-
# --- Load tokenizer and model ---
|
| 7 |
-
tokenizer = AutoTokenizer.from_pretrained("unsloth/Qwen3-1.7B")
|
| 8 |
-
base_model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
-
"unsloth/Qwen3-1.7B",
|
| 10 |
-
torch_dtype=torch.float32,
|
| 11 |
-
device_map={"": "cpu"}
|
| 12 |
-
)
|
| 13 |
-
model = PeftModel.from_pretrained(base_model, "khazarai/BioGenesis-ToT")
|
| 14 |
-
model = model.to("cpu")
|
| 15 |
-
|
| 16 |
-
# --- Define chatbot logic ---
|
| 17 |
-
def generate_response(user_input, chat_history):
|
| 18 |
-
# Append user message to history
|
| 19 |
-
chat_history.append({"role": "user", "content": user_input})
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
chat_history,
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
)
|
| 40 |
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
# Extract
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
for i, m in enumerate(chat_history[:-1])
|
| 53 |
-
if m["role"] == "user"]
|
| 54 |
-
|
| 55 |
-
return gr_chat_history, chat_history
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="emerald", secondary_hue="slate")) as demo:
|
| 58 |
gr.HTML("""
|
| 59 |
<div style="text-align: center; margin-bottom: 20px;">
|
| 60 |
<h1 style="font-family: 'Inter', sans-serif; font-weight: 800; color: #047857; font-size: 2.2em;">
|
| 61 |
-
🧬 BioGenesis-ToT Chatbot
|
| 62 |
</h1>
|
| 63 |
<p style="color: #4B5563; font-size: 1.05em; margin-top: -10px;">
|
| 64 |
-
|
| 65 |
</p>
|
| 66 |
</div>
|
| 67 |
""")
|
| 68 |
|
| 69 |
with gr.Row():
|
| 70 |
with gr.Column(scale=6):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
chatbot = gr.Chatbot(
|
| 72 |
label="BioGenesis Chat",
|
| 73 |
height=550,
|
| 74 |
bubble_full_width=False,
|
| 75 |
show_copy_button=True,
|
| 76 |
avatar_images=(
|
| 77 |
-
"https://
|
| 78 |
-
"https://cdn-icons-png.flaticon.com/512/4140/4140048.png",
|
| 79 |
),
|
| 80 |
)
|
|
|
|
| 81 |
user_input = gr.Textbox(
|
| 82 |
placeholder="Ask me about cell biology, molecular structure, or biochemistry...",
|
| 83 |
label="💬 Your question",
|
| 84 |
lines=3,
|
| 85 |
autofocus=True,
|
| 86 |
)
|
|
|
|
|
|
|
|
|
|
| 87 |
with gr.Row():
|
| 88 |
send_btn = gr.Button("🚀 Send", variant="primary")
|
| 89 |
clear_btn = gr.Button("🧹 Clear Chat")
|
| 90 |
|
| 91 |
state = gr.State([])
|
| 92 |
|
| 93 |
-
send_btn.click(generate_response, [user_input, state], [chatbot, state])
|
| 94 |
-
user_input.submit(generate_response, [user_input, state], [chatbot, state])
|
| 95 |
-
clear_btn.click(lambda: ([], []), None, [chatbot, state])
|
| 96 |
|
| 97 |
gr.HTML("""
|
| 98 |
<div style="text-align: center; margin-top: 25px; color: #6B7280; font-size: 0.9em;">
|
| 99 |
-
Powered by <b>
|
| 100 |
</div>
|
| 101 |
""")
|
| 102 |
|
| 103 |
-
demo.launch(share=True)
|
| 104 |
-
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
+
def generate_response(user_input, chat_history, hf_token):
|
| 4 |
+
if not hf_token:
|
| 5 |
+
return chat_history, chat_history, "❌ Please enter your Hugging Face API token first."
|
| 6 |
+
|
| 7 |
+
if not user_input.strip():
|
| 8 |
+
return chat_history, chat_history, ""
|
| 9 |
+
|
| 10 |
+
model_id = "khazarai/BioGenesis-ToT" # Your hosted model
|
| 11 |
+
|
| 12 |
+
headers = {
|
| 13 |
+
"Authorization": f"Bearer {hf_token}"
|
| 14 |
+
}
|
| 15 |
|
| 16 |
+
# Combine chat history into a conversation string
|
| 17 |
+
conversation = ""
|
| 18 |
+
for msg in chat_history:
|
| 19 |
+
role = "User" if msg["role"] == "user" else "Assistant"
|
| 20 |
+
conversation += f"{role}: {msg['content']}\n"
|
| 21 |
+
conversation += f"User: {user_input}\nAssistant:"
|
| 22 |
|
| 23 |
+
# Send the request to HF Inference API
|
| 24 |
+
payload = {
|
| 25 |
+
"inputs": conversation,
|
| 26 |
+
"parameters": {
|
| 27 |
+
"max_new_tokens": 2200,
|
| 28 |
+
"temperature": 0.6,
|
| 29 |
+
"top_p": 0.95,
|
| 30 |
+
"top_k": 20,
|
| 31 |
+
}
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
response = requests.post(
|
| 35 |
+
f"https://api-inference.huggingface.co/models/{model_id}",
|
| 36 |
+
headers=headers,
|
| 37 |
+
json=payload,
|
| 38 |
)
|
| 39 |
|
| 40 |
+
if response.status_code != 200:
|
| 41 |
+
return chat_history, chat_history, f"⚠️ API Error: {response.text}"
|
| 42 |
+
|
| 43 |
+
result = response.json()
|
| 44 |
|
| 45 |
+
# Extract model output
|
| 46 |
+
if isinstance(result, list) and len(result) > 0 and "generated_text" in result[0]:
|
| 47 |
+
reply = result[0]["generated_text"].split("Assistant:")[-1].strip()
|
| 48 |
+
else:
|
| 49 |
+
reply = "🤔 Sorry, I couldn’t generate a response."
|
| 50 |
+
|
| 51 |
+
chat_history.append({"role": "user", "content": user_input})
|
| 52 |
+
chat_history.append({"role": "assistant", "content": reply})
|
| 53 |
|
| 54 |
+
gr_chat_history = [
|
| 55 |
+
(m["content"], chat_history[i + 1]["content"])
|
| 56 |
+
for i, m in enumerate(chat_history[:-1])
|
| 57 |
+
if m["role"] == "user"
|
| 58 |
+
]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
+
return gr_chat_history, chat_history, ""
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
# --- UI Design ---
|
| 64 |
with gr.Blocks(theme=gr.themes.Soft(primary_hue="emerald", secondary_hue="slate")) as demo:
|
| 65 |
gr.HTML("""
|
| 66 |
<div style="text-align: center; margin-bottom: 20px;">
|
| 67 |
<h1 style="font-family: 'Inter', sans-serif; font-weight: 800; color: #047857; font-size: 2.2em;">
|
| 68 |
+
🧬 BioGenesis-ToT Chatbot (Hosted on Hugging Face)
|
| 69 |
</h1>
|
| 70 |
<p style="color: #4B5563; font-size: 1.05em; margin-top: -10px;">
|
| 71 |
+
Talk to your biology-trained LLM — no GPU needed, just your Hugging Face token ⚡
|
| 72 |
</p>
|
| 73 |
</div>
|
| 74 |
""")
|
| 75 |
|
| 76 |
with gr.Row():
|
| 77 |
with gr.Column(scale=6):
|
| 78 |
+
hf_token = gr.Textbox(
|
| 79 |
+
placeholder="Enter your Hugging Face API Token here...",
|
| 80 |
+
label="🔑 Hugging Face Token",
|
| 81 |
+
type="password",
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
chatbot = gr.Chatbot(
|
| 85 |
label="BioGenesis Chat",
|
| 86 |
height=550,
|
| 87 |
bubble_full_width=False,
|
| 88 |
show_copy_button=True,
|
| 89 |
avatar_images=(
|
| 90 |
+
"https://cdn-icons-png.flaticon.com/512/1077/1077012.png",
|
| 91 |
+
"https://cdn-icons-png.flaticon.com/512/4140/4140048.png",
|
| 92 |
),
|
| 93 |
)
|
| 94 |
+
|
| 95 |
user_input = gr.Textbox(
|
| 96 |
placeholder="Ask me about cell biology, molecular structure, or biochemistry...",
|
| 97 |
label="💬 Your question",
|
| 98 |
lines=3,
|
| 99 |
autofocus=True,
|
| 100 |
)
|
| 101 |
+
|
| 102 |
+
status_box = gr.Textbox(label="Status", interactive=False)
|
| 103 |
+
|
| 104 |
with gr.Row():
|
| 105 |
send_btn = gr.Button("🚀 Send", variant="primary")
|
| 106 |
clear_btn = gr.Button("🧹 Clear Chat")
|
| 107 |
|
| 108 |
state = gr.State([])
|
| 109 |
|
| 110 |
+
send_btn.click(generate_response, [user_input, state, hf_token], [chatbot, state, status_box])
|
| 111 |
+
user_input.submit(generate_response, [user_input, state, hf_token], [chatbot, state, status_box])
|
| 112 |
+
clear_btn.click(lambda: ([], [], ""), None, [chatbot, state, status_box])
|
| 113 |
|
| 114 |
gr.HTML("""
|
| 115 |
<div style="text-align: center; margin-top: 25px; color: #6B7280; font-size: 0.9em;">
|
| 116 |
+
Powered by <b>Hugging Face Inference API</b> | Built with ❤️ using Gradio
|
| 117 |
</div>
|
| 118 |
""")
|
| 119 |
|
| 120 |
+
demo.launch(share=True)
|
|
|