DSDUDEd commited on
Commit
ec2ed92
Β·
verified Β·
1 Parent(s): 64b74f4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -21
app.py CHANGED
@@ -1,34 +1,47 @@
1
  # app.py
2
- import gradio as gr
3
- from transformers import AutoTokenizer, AutoModelForCausalLM
4
  import torch
 
 
 
 
 
 
 
 
 
5
 
6
- # Hugging Face model repo
7
  MODEL_REPO = "DSDUDEd/firebase"
8
 
9
- # Load tokenizer and model
10
- tokenizer = AutoTokenizer.from_pretrained(MODEL_REPO)
11
- model = AutoModelForCausalLM.from_pretrained(MODEL_REPO)
 
 
 
12
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
13
  model.to(device)
14
 
15
- def generate_response(prompt, max_tokens=100):
16
- """Generate text from the model."""
17
- inputs = tokenizer(prompt, return_tensors="pt").to(device)
18
- outputs = model.generate(inputs["input_ids"], max_new_tokens=max_tokens)
 
 
19
  return tokenizer.decode(outputs[0], skip_special_tokens=True)
20
 
21
- # Gradio interface
 
 
22
  iface = gr.Interface(
23
- fn=generate_response,
24
- inputs=[
25
- gr.Textbox(label="Input Prompt"),
26
- gr.Slider(minimum=10, maximum=500, step=10, label="Max Tokens")
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
- if __name__ == "__main__":
34
- iface.launch()
 
 
 
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()