Richard Neuschulz
commited on
Commit
·
411ebc8
1
Parent(s):
a8976c8
new attempt on zero
Browse files
app.py
CHANGED
@@ -1,12 +1,14 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
|
|
3 |
|
4 |
# Load the model and tokenizer from Hugging Face
|
5 |
model_id = "seedboxai/KafkaLM-8x7B-German-V0.1-DPO"
|
6 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
7 |
-
model = AutoModelForCausalLM.from_pretrained(model_id, load_in_4bit=True, load_in_8bit=False, trust_remote_code=True)
|
8 |
|
9 |
# Define the text generation function
|
|
|
10 |
def generate_text(user_input, system_prompt):
|
11 |
# Combine the system prompt and the user input to form the full prompt
|
12 |
full_prompt = f"{system_prompt.strip()}\n\n{user_input.strip()}"
|
@@ -14,7 +16,7 @@ def generate_text(user_input, system_prompt):
|
|
14 |
# Initialize the pipeline for text generation
|
15 |
text_generator = pipeline('text-generation', model=model, tokenizer=tokenizer,
|
16 |
return_full_text=True, temperature=0.5,
|
17 |
-
max_new_tokens=512, top_p=0.95, top_k=50, do_sample=True)
|
18 |
|
19 |
# Generate text based on the full prompt
|
20 |
results = text_generator(full_prompt)
|
@@ -24,7 +26,7 @@ def generate_text(user_input, system_prompt):
|
|
24 |
|
25 |
# Setup the Gradio interface
|
26 |
iface = gr.Interface(fn=generate_text,
|
27 |
-
inputs=[gr.Textbox(lines=2, label="User Prompt"), gr.Textbox(lines=5, label="System Prompt",text="Du bist ein freundlicher und hilfsbereiter KI-Assistent. Du beantwortest Fragen faktenorientiert und präzise, ohne dabei relevante Fakten auszulassen.")],
|
28 |
outputs=gr.Textbox(label="Generated Text"),
|
29 |
title="Text Generation with KafkaLM",
|
30 |
description="Enter a user prompt and a system prompt to generate text using the KafkaLM model.")
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
3 |
+
import spaces # Import the spaces module for ZeroGPU compatibility
|
4 |
|
5 |
# Load the model and tokenizer from Hugging Face
|
6 |
model_id = "seedboxai/KafkaLM-8x7B-German-V0.1-DPO"
|
7 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
8 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, load_in_4bit=True, load_in_8bit=False, trust_remote_code=True).to('cuda') # Move the model to GPU
|
9 |
|
10 |
# Define the text generation function
|
11 |
+
@spaces.GPU # Decorate this function to use GPU
|
12 |
def generate_text(user_input, system_prompt):
|
13 |
# Combine the system prompt and the user input to form the full prompt
|
14 |
full_prompt = f"{system_prompt.strip()}\n\n{user_input.strip()}"
|
|
|
16 |
# Initialize the pipeline for text generation
|
17 |
text_generator = pipeline('text-generation', model=model, tokenizer=tokenizer,
|
18 |
return_full_text=True, temperature=0.5,
|
19 |
+
max_new_tokens=512, top_p=0.95, top_k=50, do_sample=True, device=0) # Specify the device for the pipeline
|
20 |
|
21 |
# Generate text based on the full prompt
|
22 |
results = text_generator(full_prompt)
|
|
|
26 |
|
27 |
# Setup the Gradio interface
|
28 |
iface = gr.Interface(fn=generate_text,
|
29 |
+
inputs=[gr.Textbox(lines=2, label="User Prompt"), gr.Textbox(lines=5, label="System Prompt", text="Du bist ein freundlicher und hilfsbereiter KI-Assistent. Du beantwortest Fragen faktenorientiert und präzise, ohne dabei relevante Fakten auszulassen.")],
|
30 |
outputs=gr.Textbox(label="Generated Text"),
|
31 |
title="Text Generation with KafkaLM",
|
32 |
description="Enter a user prompt and a system prompt to generate text using the KafkaLM model.")
|