Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,69 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
|
|
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
|
|
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 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 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
-
chatbot = gr.ChatInterface(
|
| 47 |
-
respond,
|
| 48 |
-
type="messages",
|
| 49 |
-
additional_inputs=[
|
| 50 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 51 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 52 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 53 |
-
gr.Slider(
|
| 54 |
-
minimum=0.1,
|
| 55 |
-
maximum=1.0,
|
| 56 |
-
value=0.95,
|
| 57 |
-
step=0.05,
|
| 58 |
-
label="Top-p (nucleus sampling)",
|
| 59 |
-
),
|
| 60 |
-
],
|
| 61 |
-
)
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
gr.LoginButton()
|
| 66 |
-
chatbot.render()
|
| 67 |
|
|
|
|
| 68 |
|
| 69 |
-
if __name__ == "__main__":
|
| 70 |
-
demo.launch()
|
|
|
|
| 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.float16,
|
| 11 |
+
device_map="auto"
|
| 12 |
+
)
|
| 13 |
+
model = PeftModel.from_pretrained(base_model, "khazarai/BioGenesis-ToT")
|
| 14 |
|
| 15 |
+
# --- Define chatbot logic ---
|
| 16 |
+
def generate_response(user_input, chat_history):
|
| 17 |
+
# Append user message to history
|
| 18 |
+
chat_history.append({"role": "user", "content": user_input})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
# Convert history to prompt
|
| 21 |
+
text = tokenizer.apply_chat_template(
|
| 22 |
+
chat_history,
|
| 23 |
+
tokenize=False,
|
| 24 |
+
add_generation_prompt=True,
|
| 25 |
+
enable_thinking=True,
|
| 26 |
+
)
|
| 27 |
|
| 28 |
+
# Tokenize and send to GPU
|
| 29 |
+
inputs = tokenizer(text, return_tensors="pt").to("cuda")
|
| 30 |
|
| 31 |
+
# Generate
|
| 32 |
+
output_tokens = model.generate(
|
| 33 |
+
**inputs,
|
| 34 |
+
max_new_tokens=1200,
|
| 35 |
+
temperature=0.6,
|
| 36 |
+
top_p=0.95,
|
| 37 |
+
top_k=20,
|
| 38 |
+
)
|
| 39 |
|
| 40 |
+
# Decode output
|
| 41 |
+
response = tokenizer.decode(output_tokens[0], skip_special_tokens=True)
|
| 42 |
|
| 43 |
+
# Extract only model's reply (avoid repeating prompt)
|
| 44 |
+
response = response.split(user_input)[-1].strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
# Add model reply to chat history
|
| 47 |
+
chat_history.append({"role": "assistant", "content": response})
|
| 48 |
+
|
| 49 |
+
# Prepare Gradio display format
|
| 50 |
+
gr_chat_history = [(m["content"], chat_history[i+1]["content"])
|
| 51 |
+
for i, m in enumerate(chat_history[:-1])
|
| 52 |
+
if m["role"] == "user"]
|
| 53 |
+
|
| 54 |
+
return gr_chat_history, chat_history
|
| 55 |
|
| 56 |
+
# --- Launch Gradio interface ---
|
| 57 |
+
with gr.Blocks() as demo:
|
| 58 |
+
gr.Markdown("## 🧬 BioGenesis-ToT Chatbot")
|
| 59 |
+
chatbot = gr.Chatbot(label="BioGenesis Chatbot", height=500)
|
| 60 |
+
user_input = gr.Textbox(placeholder="Ask a biology question...", label="Your message")
|
| 61 |
+
clear = gr.Button("Clear Chat")
|
| 62 |
|
| 63 |
+
state = gr.State([]) # Keeps chat history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
+
user_input.submit(generate_response, [user_input, state], [chatbot, state])
|
| 66 |
+
clear.click(lambda: ([], []), None, [chatbot, state])
|
|
|
|
|
|
|
| 67 |
|
| 68 |
+
demo.launch(share=True)
|
| 69 |
|
|
|
|
|
|