Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,5 +1,4 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from huggingface_hub import InferenceClient
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
import torch
|
| 5 |
import os
|
|
@@ -7,78 +6,41 @@ import os
|
|
| 7 |
# Replace 'your_huggingface_token' with your actual Hugging Face access token
|
| 8 |
access_token = os.getenv('token')
|
| 9 |
|
| 10 |
-
#
|
| 11 |
tokenizer = AutoTokenizer.from_pretrained("google/gemma-2b-it", use_auth_token=access_token)
|
| 12 |
model = AutoModelForCausalLM.from_pretrained(
|
| 13 |
"google/gemma-2b-it",
|
| 14 |
torch_dtype=torch.bfloat16,
|
| 15 |
-
|
|
|
|
| 16 |
)
|
| 17 |
-
model.eval() # Set the model to evaluation mode
|
| 18 |
|
| 19 |
-
#
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
# Generate a response with the model
|
| 28 |
-
outputs = model.generate(input_ids, max_new_tokens=2048)
|
| 29 |
-
|
| 30 |
-
# Decode and return the generated response
|
| 31 |
-
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 32 |
-
|
| 33 |
-
def respond(
|
| 34 |
-
message: str,
|
| 35 |
-
history: list[tuple[str, str]],
|
| 36 |
-
system_message: str,
|
| 37 |
-
max_tokens: int,
|
| 38 |
-
temperature: float,
|
| 39 |
-
top_p: float,
|
| 40 |
-
):
|
| 41 |
-
"""Generate a response for a multi-turn chat conversation."""
|
| 42 |
-
# Prepare the messages in the correct format for the API
|
| 43 |
-
messages = [{"role": "system", "content": system_message}]
|
| 44 |
-
|
| 45 |
-
for user_input, assistant_reply in history:
|
| 46 |
-
if user_input:
|
| 47 |
-
messages.append({"role": "user", "content": user_input})
|
| 48 |
-
if assistant_reply:
|
| 49 |
-
messages.append({"role": "assistant", "content": assistant_reply})
|
| 50 |
-
|
| 51 |
-
messages.append({"role": "user", "content": message})
|
| 52 |
-
|
| 53 |
-
# Get the complete response at once (no streaming)
|
| 54 |
-
response = client.chat_completion(
|
| 55 |
-
model="google/gemma-2b-it",
|
| 56 |
-
messages=messages,
|
| 57 |
-
max_tokens=max_tokens,
|
| 58 |
-
stream=False,
|
| 59 |
temperature=temperature,
|
| 60 |
top_p=top_p,
|
|
|
|
| 61 |
)
|
| 62 |
-
|
| 63 |
-
# Extract and return the full response
|
| 64 |
-
return response["choices"][0]["message"]["content"]
|
| 65 |
|
| 66 |
-
#
|
| 67 |
-
|
| 68 |
-
fn=
|
| 69 |
-
|
| 70 |
-
gr.Textbox(
|
| 71 |
-
gr.Slider(
|
| 72 |
-
gr.Slider(
|
| 73 |
-
gr.Slider(
|
| 74 |
-
minimum=0.1,
|
| 75 |
-
maximum=1.0,
|
| 76 |
-
value=0.95,
|
| 77 |
-
step=0.05,
|
| 78 |
-
label="Top-p (nucleus sampling)",
|
| 79 |
-
),
|
| 80 |
],
|
|
|
|
|
|
|
|
|
|
| 81 |
)
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 3 |
import torch
|
| 4 |
import os
|
|
|
|
| 6 |
# Replace 'your_huggingface_token' with your actual Hugging Face access token
|
| 7 |
access_token = os.getenv('token')
|
| 8 |
|
| 9 |
+
# Load the tokenizer and model
|
| 10 |
tokenizer = AutoTokenizer.from_pretrained("google/gemma-2b-it", use_auth_token=access_token)
|
| 11 |
model = AutoModelForCausalLM.from_pretrained(
|
| 12 |
"google/gemma-2b-it",
|
| 13 |
torch_dtype=torch.bfloat16,
|
| 14 |
+
device_map="auto",
|
| 15 |
+
use_auth_token=access_token# Automatically map to GPU if available
|
| 16 |
)
|
|
|
|
| 17 |
|
| 18 |
+
# Define generation function
|
| 19 |
+
def generate_text(prompt, max_tokens=200, temperature=0.7, top_p=0.9):
|
| 20 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 21 |
+
outputs = model.generate(
|
| 22 |
+
**inputs,
|
| 23 |
+
max_new_tokens=max_tokens,
|
| 24 |
+
do_sample=True,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
temperature=temperature,
|
| 26 |
top_p=top_p,
|
| 27 |
+
pad_token_id=tokenizer.eos_token_id
|
| 28 |
)
|
| 29 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
# Define Gradio interface
|
| 32 |
+
interface = gr.Interface(
|
| 33 |
+
fn=generate_text,
|
| 34 |
+
inputs=[
|
| 35 |
+
gr.Textbox(label="Prompt", placeholder="Enter your prompt here...", lines=2),
|
| 36 |
+
gr.Slider(50, 512, value=200, label="Max Tokens"),
|
| 37 |
+
gr.Slider(0.1, 1.0, value=0.7, step=0.1, label="Temperature"),
|
| 38 |
+
gr.Slider(0.1, 1.0, value=0.9, step=0.05, label="Top-p (nucleus sampling)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
],
|
| 40 |
+
outputs=gr.Textbox(label="Generated Text"),
|
| 41 |
+
title="Gemma-2B Text Generator",
|
| 42 |
+
description="Enter a prompt and let Google's Gemma-2B-IT model generate a response."
|
| 43 |
)
|
| 44 |
|
| 45 |
+
# Launch the app
|
| 46 |
+
interface.launch()
|