Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn.functional as F
|
| 4 |
+
from transformers import AutoTokenizer, pipeline
|
| 5 |
+
from optimum.onnxruntime import ORTModelForCausalLM
|
| 6 |
+
|
| 7 |
+
# Load your custom tokenizer and ONNX model
|
| 8 |
+
# Make sure the tokenizer and model files are in the same repo
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained("Smilyai-labs/Sam-3.0-2-onnx")
|
| 10 |
+
model = ORTModelForCausalLM.from_pretrained("Smilyai-labs/Sam-3.0-2-onnx")
|
| 11 |
+
|
| 12 |
+
# Define a function to generate text
|
| 13 |
+
def generate_text(prompt, max_length=128, temperature=0.8, top_k=60, top_p=0.9):
|
| 14 |
+
# Create a text generation pipeline
|
| 15 |
+
gen_pipeline = pipeline(
|
| 16 |
+
"text-generation",
|
| 17 |
+
model=model,
|
| 18 |
+
tokenizer=tokenizer,
|
| 19 |
+
device="cpu" # ONNX models run on CPU by default
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
# Use the pipeline to generate text
|
| 23 |
+
generated_text = gen_pipeline(
|
| 24 |
+
prompt,
|
| 25 |
+
max_length=max_length,
|
| 26 |
+
temperature=temperature,
|
| 27 |
+
top_k=top_k,
|
| 28 |
+
top_p=top_p,
|
| 29 |
+
do_sample=True,
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
# Return the generated text, cleaning up any special tokens
|
| 33 |
+
return generated_text[0]["generated_text"]
|
| 34 |
+
|
| 35 |
+
# Create a Gradio interface
|
| 36 |
+
# The fn parameter points to your generation function
|
| 37 |
+
# The inputs and outputs define the UI and API endpoint
|
| 38 |
+
demo = gr.Interface(
|
| 39 |
+
fn=generate_text,
|
| 40 |
+
inputs=[
|
| 41 |
+
gr.Textbox(label="Prompt", lines=2),
|
| 42 |
+
gr.Slider(minimum=10, maximum=512, value=128, label="Max Length"),
|
| 43 |
+
gr.Slider(minimum=0.1, maximum=2.0, value=0.8, label="Temperature"),
|
| 44 |
+
gr.Slider(minimum=1, maximum=100, value=60, label="Top K"),
|
| 45 |
+
gr.Slider(minimum=0.1, maximum=1.0, value=0.9, label="Top P"),
|
| 46 |
+
],
|
| 47 |
+
outputs="text",
|
| 48 |
+
title="SmilyAI Sam 3.0-2 ONNX Text Generation",
|
| 49 |
+
description="A simple API and UI for text generation using the ONNX version of Sam 3.0-2."
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
# Launch the Gradio app
|
| 53 |
+
demo.launch()
|