File size: 1,223 Bytes
55bc8fa
 
f975c73
55bc8fa
f975c73
55bc8fa
 
f975c73
55bc8fa
64960bc
55bc8fa
b306d99
ea883be
b306d99
64960bc
 
55bc8fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f975c73
 
55bc8fa
f975c73
55bc8fa
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
# -*- 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()