Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,33 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
# Initialize the
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def respond(
|
| 11 |
message: str,
|
|
@@ -15,7 +37,7 @@ def respond(
|
|
| 15 |
temperature: float,
|
| 16 |
top_p: float,
|
| 17 |
):
|
| 18 |
-
"""Generate a response for
|
| 19 |
# Prepare the messages in the correct format for the API
|
| 20 |
messages = [{"role": "system", "content": system_message}]
|
| 21 |
|
|
@@ -30,23 +52,20 @@ def respond(
|
|
| 30 |
response = ""
|
| 31 |
|
| 32 |
# Stream response tokens from the chat completion API
|
| 33 |
-
for
|
| 34 |
messages=messages,
|
| 35 |
max_tokens=max_tokens,
|
| 36 |
stream=True,
|
| 37 |
temperature=temperature,
|
| 38 |
top_p=top_p,
|
| 39 |
):
|
| 40 |
-
token =
|
| 41 |
response += token
|
| 42 |
yield response
|
| 43 |
|
| 44 |
-
|
| 45 |
-
For information on how to customize the ChatInterface, peruse the Gradio docs: https://www.gradio.app/docs/chatinterface
|
| 46 |
-
"""
|
| 47 |
-
|
| 48 |
demo = gr.ChatInterface(
|
| 49 |
-
respond,
|
| 50 |
additional_inputs=[
|
| 51 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 52 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
+
import torch
|
| 5 |
|
| 6 |
+
# Replace 'your_huggingface_token' with your actual Hugging Face access token
|
| 7 |
+
access_token = os.getenv('token')
|
| 8 |
+
|
| 9 |
+
# Initialize the tokenizer and model with the Hugging Face access token
|
| 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 |
+
use_auth_token=access_token
|
| 15 |
+
)
|
| 16 |
+
model.eval() # Set the model to evaluation mode
|
| 17 |
+
|
| 18 |
+
# Initialize the inference client (if needed for other API-based tasks)
|
| 19 |
+
client = InferenceClient(token=access_token)
|
| 20 |
+
|
| 21 |
+
def conversation_predict(input_text):
|
| 22 |
+
"""Generate a response for single-turn input using the model."""
|
| 23 |
+
# Tokenize the input text
|
| 24 |
+
input_ids = tokenizer(input_text, return_tensors="pt").input_ids
|
| 25 |
+
|
| 26 |
+
# Generate a response with the model
|
| 27 |
+
outputs = model.generate(input_ids, max_new_tokens=2048)
|
| 28 |
+
|
| 29 |
+
# Decode and return the generated response
|
| 30 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 31 |
|
| 32 |
def respond(
|
| 33 |
message: str,
|
|
|
|
| 37 |
temperature: float,
|
| 38 |
top_p: float,
|
| 39 |
):
|
| 40 |
+
"""Generate a response for a multi-turn chat conversation."""
|
| 41 |
# Prepare the messages in the correct format for the API
|
| 42 |
messages = [{"role": "system", "content": system_message}]
|
| 43 |
|
|
|
|
| 52 |
response = ""
|
| 53 |
|
| 54 |
# Stream response tokens from the chat completion API
|
| 55 |
+
for message_chunk in client.chat_completion(
|
| 56 |
messages=messages,
|
| 57 |
max_tokens=max_tokens,
|
| 58 |
stream=True,
|
| 59 |
temperature=temperature,
|
| 60 |
top_p=top_p,
|
| 61 |
):
|
| 62 |
+
token = message_chunk["choices"][0]["delta"].get("content", "")
|
| 63 |
response += token
|
| 64 |
yield response
|
| 65 |
|
| 66 |
+
# Create a Gradio ChatInterface demo
|
|
|
|
|
|
|
|
|
|
| 67 |
demo = gr.ChatInterface(
|
| 68 |
+
fn=respond,
|
| 69 |
additional_inputs=[
|
| 70 |
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 71 |
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|