Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
model_id = "nvidia/OpenReasoning-Nemotron-1.5B"
|
| 6 |
+
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 8 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32)
|
| 9 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 10 |
+
model.to(device)
|
| 11 |
+
|
| 12 |
+
def chat_api(prompt, max_new_tokens=200, temperature=0.7):
|
| 13 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(device)
|
| 14 |
+
outputs = model.generate(
|
| 15 |
+
**inputs,
|
| 16 |
+
max_new_tokens=max_new_tokens,
|
| 17 |
+
temperature=temperature,
|
| 18 |
+
do_sample=True
|
| 19 |
+
)
|
| 20 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 21 |
+
return response
|
| 22 |
+
|
| 23 |
+
demo = gr.Interface(
|
| 24 |
+
fn=chat_api,
|
| 25 |
+
inputs=[
|
| 26 |
+
gr.Textbox(label="Prompt", placeholder="Ask me anything..."),
|
| 27 |
+
gr.Slider(50, 512, value=200, step=10, label="Max Tokens"),
|
| 28 |
+
gr.Slider(0.1, 1.5, value=0.7, step=0.1, label="Temperature")
|
| 29 |
+
],
|
| 30 |
+
outputs="text",
|
| 31 |
+
title="OpenReasoning Nemotron-1.5B API",
|
| 32 |
+
description="Public Hugging Face Space that runs NVIDIA's Nemotron-1.5B model."
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
demo.launch()
|