science-chat / app.py
arinbalyan's picture
Upload folder using huggingface_hub
16dff3b verified
Raw
History Blame Contribute Delete
2.74 kB
import gradio as gr
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
MODEL_ID = "arinbalyan/deepseek-science"
tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token
model = AutoModelForCausalLM.from_pretrained(
MODEL_ID,
torch_dtype=torch.float16,
device_map="auto",
)
def generate(prompt, history):
if not prompt.strip():
return "", history
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
with torch.no_grad():
out = model.generate(
**inputs,
max_new_tokens=200,
temperature=0.8,
do_sample=True,
pad_token_id=tokenizer.eos_token_id,
)
response = tokenizer.decode(out[0][inputs.input_ids.shape[1]:], skip_special_tokens=True)
history.append((prompt, response.strip()))
return "", history
theme = (
gr.themes.Soft(primary_hue="teal", neutral_hue="slate")
.set(
button_primary_background_fill_hover="#0d9488",
button_primary_text_color="white",
input_background_fill="#f8fafc",
input_border_color="#e2e8f0",
input_border_color_focus="#0d9488",
)
)
css = """.gradio-container { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif !important; }
.main-header { text-align: center; padding: 2rem 1rem; background: linear-gradient(135deg, #0d9488 0%, #0d9488dd 100%); color: white; border-radius: 12px; margin-bottom: 1rem; }
.main-header h1 { font-size: 2rem; font-weight: 700; margin-bottom: 0.25rem; }
.main-header p { font-size: 1rem; opacity: 0.9; }
.footer { text-align: center; padding: 1rem; color: #64748b; font-size: 0.875rem; }"""
with gr.Blocks(title="Science Tutor") as demo:
with gr.Row():
with gr.Column():
gr.HTML(f"""<div class="main-header"><h1>🔬 Science Tutor</h1><p>Generate with AI</p></div>""")
chatbot = gr.Chatbot(height=400)
msg = gr.Textbox(label="Prompt", placeholder='e.g., What is photosynthesis?', lines=2)
with gr.Row():
submit = gr.Button("Generate", variant="primary", size="lg", scale=1)
clear = gr.Button("Clear", size="lg", scale=0)
gr.Examples(examples=["What is photosynthesis?", "Explain Newton's laws of motion", "How does DNA replication work?", "What causes the seasons on Earth?", "Explain the periodic table trends"], inputs=msg, label="Try these")
gr.HTML(f"""<div class="footer">DeepSeek-R1-Distill-Qwen-1.5B on science data</div>""")
submit.click(generate, [msg, chatbot], [msg, chatbot])
clear.click(lambda: None, None, chatbot, queue=False)
if __name__ == "__main__":
demo.launch(theme=theme, css=css)