Spaces:
Runtime error
Runtime error
# -*- coding: utf-8 -*- | |
"""Hugging Face.ipynb | |
Automatically generated by Colab. | |
Original file is located at | |
https://colab.research.google.com/drive/1zRuAxGm_11lNIeBxFlHVzc5tNKhyLef4 | |
""" | |
import gradio as gr | |
import os | |
# 加載 LLaMA 模型 | |
from transformers import AutoTokenizer, AutoModelForCausalLM | |
token = os.getenv("Git Access") | |
tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-2-7b-chat-hf",use_auth_token=token) | |
model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-2-7b-chat-hf",use_auth_token=token) | |
# 定義推理函數 | |
def generate_text(prompt): | |
inputs = tokenizer(prompt, return_tensors="pt") | |
outputs = model.generate( | |
inputs.input_ids, | |
max_length=200, | |
num_beams=5, | |
repetition_penalty=1.2, | |
early_stopping=True | |
) | |
return tokenizer.decode(outputs[0], skip_special_tokens=True) | |
# 使用 Gradio 构建界面 | |
interface = gr.Interface( | |
fn=generate_text, | |
inputs=gr.Textbox(lines=5, placeholder="Enter your prompt here..."), | |
outputs="text", | |
title="LLaMA Text Generator", | |
description="Generate text using LLaMA 2 models hosted on Hugging Face Spaces." | |
) | |
# 啟動應用 | |
if __name__ == "__main__": | |
interface.launch() |