Spaces:
Sleeping
Sleeping
Update app.py
Browse filesadding lines of code
app.py
CHANGED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
MODEL_NAME = "openlm-research/open_gpt-oss-20b"
|
| 2 |
+
|
| 3 |
+
# Load tokenizer & model
|
| 4 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
| 5 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 6 |
+
MODEL_NAME,
|
| 7 |
+
device_map="auto", # Automatically use GPU if available
|
| 8 |
+
torch_dtype="auto"
|
| 9 |
+
)
|
| 10 |
+
|
| 11 |
+
# Create generation pipeline
|
| 12 |
+
story_generator = pipeline(
|
| 13 |
+
"text-generation",
|
| 14 |
+
model=model,
|
| 15 |
+
tokenizer=tokenizer
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
+
# Function to generate stories
|
| 19 |
+
def generate_story(prompt, max_length=300, temperature=0.8):
|
| 20 |
+
outputs = story_generator(
|
| 21 |
+
prompt,
|
| 22 |
+
max_length=max_length,
|
| 23 |
+
temperature=temperature,
|
| 24 |
+
do_sample=True,
|
| 25 |
+
top_p=0.95,
|
| 26 |
+
top_k=50
|
| 27 |
+
)
|
| 28 |
+
return outputs[0]["generated_text"]
|
| 29 |
+
|
| 30 |
+
# Gradio UI
|
| 31 |
+
with gr.Blocks() as demo:
|
| 32 |
+
gr.Markdown("# 📖 Interactive Story Generator (open-gpt-oss-20b)")
|
| 33 |
+
gr.Markdown("Type a prompt and let the AI continue your story with a powerful 20B model.")
|
| 34 |
+
prompt = gr.Textbox(
|
| 35 |
+
label="Your Story Prompt",
|
| 36 |
+
placeholder="e.g., In the far future, humanity discovered a hidden planet...",
|
| 37 |
+
lines=3
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
max_length = gr.Slider(50, 1000, value=300, step=50, label="Story Length")
|
| 41 |
+
temperature = gr.Slider(0.1, 1.5, value=0.8, step=0.1, label="Creativity")
|
| 42 |
+
|
| 43 |
+
generate_btn = gr.Button("✨ Generate Story")
|
| 44 |
+
output = gr.Textbox(label="Generated Story", lines=20)
|
| 45 |
+
|
| 46 |
+
generate_btn.click(
|
| 47 |
+
fn=generate_story,
|
| 48 |
+
inputs=[prompt, max_length, temperature],
|
| 49 |
+
outputs=output
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
demo.launch()
|