playground / app.py.bak
vs4vijay's picture
Rename app.py to app.py.bak
cffabb0
raw
history blame
2.97 kB
import re
import os
import requests
import gradio as gr
from datasets import load_dataset
from PIL import Image
from io import BytesIO
import torch
from torch import autocast
from transformers import pipeline, set_seed
from diffusers import DiffusionPipeline, StableDiffusionPipeline
# Config
DEVICE = "cuda"
# GPT2
def get_gpt2_pipeline():
generator = pipeline('text-generation', model='gpt2')
set_seed(42)
# generator("Hello world, I'm vizard,", max_length=50, num_return_sequences=3)
return generator
# Text Summarizer
def get_text_summarizer_pipeline():
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
# generator("Hello world, I'm vizard,", max_length=50, num_return_sequences=3)
return summarizer
# SD v1.4
def get_stable_diffusion_v14_pipeline():
model_id = "CompVis/stable-diffusion-v1-4"
pipe = StableDiffusionPipeline.from_pretrained(model_id, use_auth_token=True)
# pipeline = StableDiffusionPipeline.from_pretrained(model_id, use_auth_token=True, revision="fp16", torch_dtype=torch.float16)
pipe = pipe.to(DEVICE)
torch.backends.cudnn.benchmark = True
return pipe
# SD v1.5
def get_stable_diffusion_v15_pipeline():
model_id = "runwayml/stable-diffusion-v1-5"
pipe = DiffusionPipeline.from_pretrained(mode_id)
pipe = pipe.to(DEVICE)
return pipe
def get_image(url):
response = requests.get(url)
image = Image.open(BytesIO(response.content)).convert("RGB")
resized_image = image.resize((768, 512))
return resized_image
# main
def main():
prompt = "Hello world, I'm vizard,"
gpt2_pipe = get_gpt2_pipeline()
def greet(prompt):
return gpt2_pipe(prompt, max_length=1000, num_return_sequences=3)
with gr.Blocks() as ui:
with gr.Row():
with gr.Column():
gpt_int = gr.Interface(
fn=greet,
inputs=gr.Textbox(lines=2, placeholder="Enter some text here..."),
outputs="text",
title="GPT2",
description="OneDesc",
)
with gr.Row():
with gr.Column():
gpt_int2 = gr.Interface(
fn=greet,
inputs=gr.Textbox(lines=2, placeholder="Enter some text here..."),
outputs="text",
title="GPT2",
description="OneDesc",
)
gr.Examples(['one.png', 'two.png', 'three.jpeg'])
# ui = gr.Interface.from_pipeline(
# get_text_summarizer_pipeline(),
# title="OneTitle",
# description="OneDesc",
# examples=['one.png', 'two.png', 'three.jpeg'],
# )
ui.launch(enable_queue=True)
# pipe = pipeline(task="image-classification", model="microsoft/dit-base-finetuned-rvlcdip")
#gr.Interface.from_pipeline(
# pipe,
# title="OneTitle",
# description="OneDescription",
# examples=['one.png', 'two.png', 'three.jpeg'],
# ).launch()
# pipe2 = get_stable_diffusion_v15_pipeline()
# images = pipe2(prompt).images
main()