File size: 1,689 Bytes
5bb063f
 
 
 
f88f44b
 
 
5bb063f
f88f44b
5bb063f
 
 
f88f44b
5bb063f
 
 
f88f44b
 
 
 
5bb063f
f88f44b
5bb063f
f88f44b
5bb063f
 
f88f44b
 
 
 
5bb063f
 
f88f44b
5bb063f
f88f44b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5bb063f
 
 
e977630
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import gradio as gr
import torch
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM

# 1. Load a model that fits in the free tier
# 'flan-t5-large' is powerful but small enough for the free CPU tier
model_name = 'google/flan-t5-large'

print("Loading model... this may take a minute.")
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)

# Use GPU if available, otherwise CPU
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = model.to(device)

# 2. Define the generation function
def generate_text(task_prefix, input_text):
    # Dynamic prompt construction
    prompt = f"{task_prefix}: {input_text}"
    
    inputs = tokenizer(prompt, return_tensors="pt").to(device)
    
    # Generate output
    output_ids = model.generate(
        **inputs,
        max_length=64,
        num_beams=4,
        early_stopping=True,
        no_repeat_ngram_size=2
    )
    
    return tokenizer.decode(output_ids[0], skip_special_tokens=True)

# 3. Create the Web Interface
# We map the choices to the actual prefixes the model understands
task_choices = [
    "summarize", 
    "translate French to English", 
    "paraphrase", 
    "generate question", 
    "sst2 sentence"
]

demo = gr.Interface(
    fn=generate_text,
    inputs=[
        gr.Dropdown(choices=task_choices, label="Select Task", value="summarize"),
        gr.Textbox(label="Input Text", placeholder="Enter your text here...")
    ],
    outputs=gr.Textbox(label="AI Output"),
    title="Multi-Task AI Generator",
    description="Select a task and enter text. Powered by Google Flan-T5."
)

# 4. Launch
demo.launch(ssr_mode=False)