Spaces:
Runtime error
Runtime error
| from transformers import AutoTokenizer, AutoModelForSeq2SeqLM | |
| from transformers import pipeline | |
| import torch | |
| import gradio as gr | |
| # chatgpt-gpt4-prompts-bart-large-cnn-samsum | |
| tokenizer = AutoTokenizer.from_pretrained( | |
| "Kaludi/chatgpt-gpt4-prompts-bart-large-cnn-samsum") | |
| model = AutoModelForSeq2SeqLM.from_pretrained( | |
| "Kaludi/chatgpt-gpt4-prompts-bart-large-cnn-samsum", from_tf=True) | |
| # zephyr | |
| pipe = pipeline("text-generation", model="HuggingFaceH4/zephyr-7b-alpha", | |
| torch_dtype=torch.bfloat16, device_map="auto") | |
| def useZephyr(prompt): | |
| messages = [ | |
| { | |
| "role": "system", | |
| "content": "you are a chatbot who always responds politely and in the shortest possible way", | |
| }, | |
| {"role": "user", "content": prompt}, | |
| ] | |
| # https://huggingface.co/docs/transformers/main/en/chat_templating | |
| prompt = pipe.tokenizer.apply_chat_template( | |
| messages, tokenize=False, add_generation_prompt=True) | |
| outputs = pipe(prompt) | |
| return outputs[0]["generated_text"] | |
| def generatePrompt(prompt): | |
| batch = tokenizer(prompt, return_tensors="pt") | |
| generated_ids = model.generate(batch["input_ids"]) | |
| output = tokenizer.batch_decode(generated_ids, skip_special_tokens=True) | |
| prompt = output[0] | |
| result = useZephyr(prompt) | |
| return result | |
| # | |
| # Interface | |
| input_prompt = gr.Textbox(label="Prompt", value="photographer") | |
| output_component = gr.Textbox(label="Output") | |
| examples = [["photographer"], ["developer"], ["teacher"], [ | |
| "human resources staff"], ["recipe for ham croquettes"]] | |
| description = "" | |
| PerfectGPT = gr.Interface(generatePrompt, inputs=input_prompt, outputs=output_component, | |
| examples=examples, title="馃椏 PerfectGPT v1 馃椏", description=description) | |
| PerfectGPT.launch() | |