Hemanshu121 commited on
Commit
2fc0aab
·
verified ·
1 Parent(s): bc1dd60

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -0
app.py ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import AutoTokenizer, AutoModelForCausalLM
3
+ import gradio as gr
4
+
5
+ # Load model from your uploaded model repo
6
+ model_name = "your-username/my-textgen-model" # change this to your Hugging Face model name
7
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
8
+ model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.float32)
9
+
10
+ def generate_text(prompt, max_length=200):
11
+ inputs = tokenizer(prompt, return_tensors="pt")
12
+ outputs = model.generate(**inputs, max_length=max_length)
13
+ text = tokenizer.decode(outputs[0], skip_special_tokens=True)
14
+ return text
15
+
16
+ iface = gr.Interface(
17
+ fn=generate_text,
18
+ inputs=[
19
+ gr.Textbox(label="Prompt", placeholder="Type your text here..."),
20
+ gr.Slider(50, 500, value=200, label="Max Length")
21
+ ],
22
+ outputs="text",
23
+ title="Text Generation API",
24
+ description="PyTorch text generation model deployed on Hugging Face"
25
+ )
26
+
27
+ iface.launch()