File size: 1,025 Bytes
55bc8fa
3e37a08
f975c73
55bc8fa
b306d99
 
3e37a08
 
 
55bc8fa
 
 
 
 
 
 
 
 
 
 
 
 
3e37a08
55bc8fa
 
 
 
3e37a08
 
f975c73
 
55bc8fa
f975c73
3e37a08
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
# -*- coding: utf-8 -*-
"""Hugging Face Llama 2 App"""

import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM

# 加載 LLaMA-2 模型
tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-2-13b-chat-hf")
model = AutoModelForCausalLM.from_pretrained("meta-llama/Llama-2-13b-chat-hf")

# 定義推理函數
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 2 Text Generator",
    description="Generate text using the Llama-2-13b-chat-hf model hosted on Hugging Face Spaces."
)

# 啟動應用
if __name__ == "__main__":
    interface.launch()