Spaces:
Sleeping
Sleeping
PierreJousselin
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,36 +1,26 @@
|
|
1 |
import gradio as gr
|
2 |
-
from
|
3 |
-
from transformers import AutoTokenizer
|
4 |
|
5 |
-
#
|
6 |
-
model_name = "
|
7 |
-
|
8 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
|
10 |
-
# Define
|
11 |
-
def chat_with_model(
|
12 |
-
#
|
13 |
-
inputs = tokenizer(
|
14 |
-
inputs["pad_token_id"] = tokenizer.pad_token_id # Set the pad token ID
|
15 |
-
# Send the request to the Hugging Face Inference API
|
16 |
-
result = client.text_generation(
|
17 |
-
prompt=inputs["input_ids"].tolist(), # Send tokenized input
|
18 |
-
)
|
19 |
|
20 |
-
#
|
21 |
-
|
|
|
|
|
|
|
22 |
|
23 |
return response
|
24 |
|
25 |
-
# Set up
|
26 |
-
|
27 |
-
fn=chat_with_model,
|
28 |
-
inputs=[gr.Textbox(lines=5, placeholder="Enter your text here...", label="Input Text")],
|
29 |
-
outputs=gr.Textbox(lines=5, label="Response"),
|
30 |
-
title="Hugging Face Chatbot",
|
31 |
-
description="A simple chatbot powered by Hugging Face and InferenceClient."
|
32 |
-
)
|
33 |
|
34 |
-
# Launch the
|
35 |
-
|
36 |
-
interface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
|
|
3 |
|
4 |
+
# Load the model and tokenizer from Hugging Face
|
5 |
+
model_name = "your_huggingface_model_name" # Replace with your model's name or path
|
6 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
7 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
|
9 |
+
# Define the chat function
|
10 |
+
def chat_with_model(user_input):
|
11 |
+
# Encode the input
|
12 |
+
inputs = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors="pt")
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
+
# Generate a response from the model
|
15 |
+
outputs = model.generate(inputs, max_length=1000, pad_token_id=tokenizer.eos_token_id)
|
16 |
+
|
17 |
+
# Decode the model's output
|
18 |
+
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
19 |
|
20 |
return response
|
21 |
|
22 |
+
# Set up Gradio interface
|
23 |
+
iface = gr.Interface(fn=chat_with_model, inputs="text", outputs="text", live=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
+
# Launch the interface
|
26 |
+
iface.launch()
|
|