t
File size: 1,006 Bytes
7165a01
5979a46
7165a01
5979a46
 
 
 
7165a01
 
 
5979a46
7165a01
 
 
 
 
 
 
 
 
 
 
 
 
 
5979a46
 
7165a01
 
 
 
 
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
import gradio as gr
from transformers import AutoTokenizer, AutoModelForCausalLM

# Modellname
model_name = "meta-llama/Llama-3.1-8B-Instruct"

# Tokenizer und Modell laden
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
    model_name,
    device_map=None,  # Keine GPU
    torch_dtype="float32"  # Float32 für CPU
)

# Funktion für die Textgenerierung
def generate_response(prompt):
    inputs = tokenizer(prompt, return_tensors="pt", truncation=True)
    outputs = model.generate(inputs["input_ids"], max_length=200, num_beams=5, early_stopping=True)
    return tokenizer.decode(outputs[0], skip_special_tokens=True)

# Gradio-Interface erstellen
interface = gr.Interface(
    fn=generate_response,
    inputs="text",
    outputs="text",
    title="LLaMA 3.1 8B Instruct Text Generator (CPU)",
    description="Gib einen Text ein, und LLaMA 3.1 8B Instruct generiert eine Antwort."
)

# App starten
if __name__ == "__main__":
    interface.launch()