Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,47 @@
|
|
| 1 |
# app.py
|
| 2 |
-
import
|
| 3 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
# Hugging Face model repo
|
| 7 |
MODEL_REPO = "DSDUDEd/firebase"
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
| 12 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 13 |
model.to(device)
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
| 19 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 20 |
|
| 21 |
-
#
|
|
|
|
|
|
|
| 22 |
iface = gr.Interface(
|
| 23 |
-
fn=
|
| 24 |
-
inputs=[
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
outputs=gr.Textbox(label="Model Output"),
|
| 29 |
-
title="Custom GPT-2 AI",
|
| 30 |
-
description="Type a prompt and the AI will generate a response."
|
| 31 |
)
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
| 1 |
# app.py
|
| 2 |
+
import os
|
|
|
|
| 3 |
import torch
|
| 4 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 5 |
+
import gradio as gr
|
| 6 |
+
|
| 7 |
+
# -----------------------------
|
| 8 |
+
# 1οΈβ£ Hugging Face token
|
| 9 |
+
# -----------------------------
|
| 10 |
+
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 11 |
+
if HF_TOKEN is None:
|
| 12 |
+
raise ValueError("Set your Hugging Face token in the environment variable HF_TOKEN")
|
| 13 |
|
|
|
|
| 14 |
MODEL_REPO = "DSDUDEd/firebase"
|
| 15 |
|
| 16 |
+
# -----------------------------
|
| 17 |
+
# 2οΈβ£ Load model & tokenizer
|
| 18 |
+
# -----------------------------
|
| 19 |
+
tokenizer = AutoTokenizer.from_pretrained(MODEL_REPO, use_auth_token=HF_TOKEN)
|
| 20 |
+
model = AutoModelForCausalLM.from_pretrained(MODEL_REPO, use_auth_token=HF_TOKEN)
|
| 21 |
+
|
| 22 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 23 |
model.to(device)
|
| 24 |
|
| 25 |
+
# -----------------------------
|
| 26 |
+
# 3οΈβ£ Define generation function
|
| 27 |
+
# -----------------------------
|
| 28 |
+
def generate(prompt, max_tokens=50):
|
| 29 |
+
input_ids = tokenizer(prompt, return_tensors="pt").input_ids.to(device)
|
| 30 |
+
outputs = model.generate(input_ids, max_new_tokens=max_tokens)
|
| 31 |
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 32 |
|
| 33 |
+
# -----------------------------
|
| 34 |
+
# 4οΈβ£ Create Gradio interface
|
| 35 |
+
# -----------------------------
|
| 36 |
iface = gr.Interface(
|
| 37 |
+
fn=generate,
|
| 38 |
+
inputs=[gr.Textbox(lines=2, placeholder="Enter your prompt here"), gr.Slider(minimum=1, maximum=200, value=50)],
|
| 39 |
+
outputs="text",
|
| 40 |
+
title="DSDUDEd Firebase AI",
|
| 41 |
+
description="Generate text using the custom model hosted privately on Hugging Face."
|
|
|
|
|
|
|
|
|
|
| 42 |
)
|
| 43 |
|
| 44 |
+
# -----------------------------
|
| 45 |
+
# 5οΈβ£ Launch Space
|
| 46 |
+
# -----------------------------
|
| 47 |
+
iface.launch()
|