Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,6 +2,7 @@ import gradio as gr
|
|
| 2 |
import torch
|
| 3 |
from transformers import pipeline
|
| 4 |
import os
|
|
|
|
| 5 |
|
| 6 |
# --- App Configuration ---
|
| 7 |
TITLE = "✍️ AI Story Outliner"
|
|
@@ -37,16 +38,19 @@ try:
|
|
| 37 |
hf_token = os.environ.get("HF_TOKEN")
|
| 38 |
|
| 39 |
if hf_token:
|
| 40 |
-
print("✅ HF_TOKEN secret found.")
|
|
|
|
|
|
|
|
|
|
| 41 |
else:
|
| 42 |
# If no token is found, raise an error to prevent the app from crashing later.
|
| 43 |
raise ValueError("Hugging Face token not found. Please set the HF_TOKEN secret in your Space settings.")
|
| 44 |
|
| 45 |
-
# Using 'mistralai/Mistral-7B-v0.1'.
|
|
|
|
| 46 |
generator = pipeline(
|
| 47 |
"text-generation",
|
| 48 |
model="mistralai/Mistral-7B-v0.1",
|
| 49 |
-
token=hf_token,
|
| 50 |
torch_dtype=torch.bfloat16, # More performant data type
|
| 51 |
device_map="auto" # Will use GPU if available, otherwise CPU
|
| 52 |
)
|
|
@@ -102,8 +106,6 @@ The outline must have three parts: a dramatic hook, a concise ballad, and a sati
|
|
| 102 |
for out in outputs:
|
| 103 |
# The model will generate the prompt plus the continuation. We extract just the new part.
|
| 104 |
full_text = out['generated_text']
|
| 105 |
-
# Add back the part of the prompt we want to see in the output
|
| 106 |
-
story_start = "### 🎬 The Hook\n"
|
| 107 |
# Split by the instruction closing tag to get only the model's response
|
| 108 |
generated_part = full_text.split("[/INST]")[-1].strip()
|
| 109 |
stories.append(generated_part)
|
|
|
|
| 2 |
import torch
|
| 3 |
from transformers import pipeline
|
| 4 |
import os
|
| 5 |
+
from huggingface_hub import login
|
| 6 |
|
| 7 |
# --- App Configuration ---
|
| 8 |
TITLE = "✍️ AI Story Outliner"
|
|
|
|
| 38 |
hf_token = os.environ.get("HF_TOKEN")
|
| 39 |
|
| 40 |
if hf_token:
|
| 41 |
+
print("✅ HF_TOKEN secret found. Logging in...")
|
| 42 |
+
# Programmatically log in to Hugging Face. This is a more robust method.
|
| 43 |
+
login(token=hf_token)
|
| 44 |
+
print("✅ Login successful.")
|
| 45 |
else:
|
| 46 |
# If no token is found, raise an error to prevent the app from crashing later.
|
| 47 |
raise ValueError("Hugging Face token not found. Please set the HF_TOKEN secret in your Space settings.")
|
| 48 |
|
| 49 |
+
# Using 'mistralai/Mistral-7B-v0.1'.
|
| 50 |
+
# After login(), the token argument is no longer needed here as the session is authenticated.
|
| 51 |
generator = pipeline(
|
| 52 |
"text-generation",
|
| 53 |
model="mistralai/Mistral-7B-v0.1",
|
|
|
|
| 54 |
torch_dtype=torch.bfloat16, # More performant data type
|
| 55 |
device_map="auto" # Will use GPU if available, otherwise CPU
|
| 56 |
)
|
|
|
|
| 106 |
for out in outputs:
|
| 107 |
# The model will generate the prompt plus the continuation. We extract just the new part.
|
| 108 |
full_text = out['generated_text']
|
|
|
|
|
|
|
| 109 |
# Split by the instruction closing tag to get only the model's response
|
| 110 |
generated_part = full_text.split("[/INST]")[-1].strip()
|
| 111 |
stories.append(generated_part)
|