Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
import torch
|
4 |
import os
|
@@ -6,41 +7,84 @@ import os
|
|
6 |
# Replace 'your_huggingface_token' with your actual Hugging Face access token
|
7 |
access_token = os.getenv('token')
|
8 |
|
9 |
-
#
|
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 |
-
|
15 |
-
use_auth_token=access_token# Automatically map to GPU if available
|
16 |
)
|
|
|
17 |
|
18 |
-
#
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
temperature=temperature,
|
26 |
top_p=top_p,
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
30 |
|
31 |
-
#
|
32 |
-
|
33 |
-
fn=
|
34 |
-
|
35 |
-
gr.Textbox(
|
36 |
-
gr.Slider(
|
37 |
-
gr.Slider(0.1,
|
38 |
-
gr.Slider(
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
from transformers import AutoTokenizer, AutoModelForCausalLM
|
4 |
import torch
|
5 |
import os
|
|
|
7 |
# Replace 'your_huggingface_token' with your actual Hugging Face access token
|
8 |
access_token = os.getenv('token')
|
9 |
|
10 |
+
# Initialize the tokenizer and model with the Hugging Face access token
|
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 |
+
use_auth_token=access_token
|
|
|
16 |
)
|
17 |
+
model.eval() # Set the model to evaluation mode
|
18 |
|
19 |
+
# Initialize the inference client (if needed for other API-based tasks)
|
20 |
+
client = InferenceClient(provider="together",token=access_token)
|
21 |
+
|
22 |
+
def conversation_predict(input_text):
|
23 |
+
"""Generate a response for single-turn input using the model."""
|
24 |
+
# Tokenize the input text
|
25 |
+
input_ids = tokenizer(input_text, return_tensors="pt").input_ids
|
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 |
+
response = ""
|
54 |
+
|
55 |
+
# Stream response tokens from the chat completion API
|
56 |
+
for message_chunk in client.chat_completion(
|
57 |
+
model = "google/gemma-2b-it",
|
58 |
+
messages=messages,
|
59 |
+
max_tokens=max_tokens,
|
60 |
+
stream=True,
|
61 |
temperature=temperature,
|
62 |
top_p=top_p,
|
63 |
+
):
|
64 |
+
token = message_chunk["choices"][0]["delta"].get("content", "")
|
65 |
+
response += token
|
66 |
+
yield response
|
67 |
|
68 |
+
# Create a Gradio ChatInterface demo
|
69 |
+
demo = gr.ChatInterface(
|
70 |
+
fn=respond,
|
71 |
+
additional_inputs=[
|
72 |
+
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
73 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
74 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
75 |
+
gr.Slider(
|
76 |
+
minimum=0.1,
|
77 |
+
maximum=1.0,
|
78 |
+
value=0.95,
|
79 |
+
step=0.05,
|
80 |
+
label="Top-p (nucleus sampling)",
|
81 |
+
),
|
82 |
],
|
|
|
|
|
|
|
83 |
)
|
84 |
|
85 |
+
if __name__ == "__main__":
|
86 |
+
demo.launch(share=True)
|
87 |
+
|
88 |
+
|
89 |
+
|
90 |
+
do not stream the output
|