Spaces:
Runtime error
Runtime error
Martín Bravo
commited on
Commit
·
304c4d9
1
Parent(s):
e541117
add: model
Browse files- app.py +33 -34
- requirements.txt +3 -1
- test.py +16 -0
app.py
CHANGED
|
@@ -1,10 +1,20 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
"""
|
| 7 |
-
client = InferenceClient("martinbravo/llama_finetuned_test")
|
| 8 |
|
| 9 |
|
| 10 |
def respond(
|
|
@@ -15,34 +25,27 @@ def respond(
|
|
| 15 |
temperature,
|
| 16 |
top_p,
|
| 17 |
):
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
for
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
response = ""
|
| 29 |
-
|
| 30 |
-
for message in client.chat_completion(
|
| 31 |
-
messages,
|
| 32 |
-
max_tokens=max_tokens,
|
| 33 |
-
stream=True,
|
| 34 |
temperature=temperature,
|
| 35 |
top_p=top_p,
|
| 36 |
-
|
| 37 |
-
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
| 41 |
|
| 42 |
|
| 43 |
-
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
demo = gr.ChatInterface(
|
| 47 |
respond,
|
| 48 |
additional_inputs=[
|
|
@@ -51,14 +54,10 @@ demo = gr.ChatInterface(
|
|
| 51 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 52 |
gr.Slider(
|
| 53 |
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
),
|
| 59 |
],
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
| 3 |
+
|
| 4 |
+
# Load model and tokenizer
|
| 5 |
+
model_name = "martinbravo/llama_finetuned_test"
|
| 6 |
+
base_model_name = "unsloth/llama-3.2-1b-instruct-bnb-4bit"
|
| 7 |
+
|
| 8 |
+
# Load tokenizer and model locally
|
| 9 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 10 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 11 |
+
model_name,
|
| 12 |
+
device_map="auto", # Automatically maps model to GPU/CPU
|
| 13 |
+
trust_remote_code=True, # If model uses custom implementations
|
| 14 |
+
)
|
| 15 |
|
| 16 |
+
# Create a text-generation pipeline
|
| 17 |
+
generator = pipeline("text-generation", model=model, tokenizer=tokenizer, device=0)
|
|
|
|
|
|
|
| 18 |
|
| 19 |
|
| 20 |
def respond(
|
|
|
|
| 25 |
temperature,
|
| 26 |
top_p,
|
| 27 |
):
|
| 28 |
+
# Build input prompt
|
| 29 |
+
prompt = system_message + "\n"
|
| 30 |
+
for user_input, assistant_response in history:
|
| 31 |
+
prompt += f"User: {user_input}\nAssistant: {assistant_response}\n"
|
| 32 |
+
prompt += f"User: {message}\nAssistant:"
|
| 33 |
+
|
| 34 |
+
# Generate response
|
| 35 |
+
response = generator(
|
| 36 |
+
prompt,
|
| 37 |
+
max_new_tokens=max_tokens,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
temperature=temperature,
|
| 39 |
top_p=top_p,
|
| 40 |
+
do_sample=True, # Sampling for diverse responses
|
| 41 |
+
)[0]["generated_text"]
|
| 42 |
|
| 43 |
+
# Extract the assistant's response
|
| 44 |
+
assistant_response = response[len(prompt) :].strip()
|
| 45 |
+
yield assistant_response
|
| 46 |
|
| 47 |
|
| 48 |
+
# Gradio interface
|
|
|
|
|
|
|
| 49 |
demo = gr.ChatInterface(
|
| 50 |
respond,
|
| 51 |
additional_inputs=[
|
|
|
|
| 54 |
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 55 |
gr.Slider(
|
| 56 |
minimum=0.1,
|
| 57 |
+
maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"
|
|
|
|
|
|
|
|
|
|
| 58 |
),
|
| 59 |
],
|
| 60 |
)
|
| 61 |
|
|
|
|
| 62 |
if __name__ == "__main__":
|
| 63 |
+
demo.launch()
|
requirements.txt
CHANGED
|
@@ -1 +1,3 @@
|
|
| 1 |
-
huggingface_hub==0.25.2
|
|
|
|
|
|
|
|
|
| 1 |
+
huggingface_hub==0.25.2
|
| 2 |
+
gradio
|
| 3 |
+
transformers
|
test.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import AutoModel, AutoTokenizer
|
| 2 |
+
|
| 3 |
+
# Load the tokenizer
|
| 4 |
+
tokenizer = AutoTokenizer.from_pretrained("martinbravo/llama_finetuned_test")
|
| 5 |
+
|
| 6 |
+
# Load the model
|
| 7 |
+
model = AutoModel.from_pretrained("martinbravo/llama_finetuned_test")
|
| 8 |
+
|
| 9 |
+
# Test the model
|
| 10 |
+
input_text = "What is the capital of France?"
|
| 11 |
+
inputs = tokenizer(input_text, return_tensors="pt")
|
| 12 |
+
|
| 13 |
+
# Perform inference
|
| 14 |
+
outputs = model(**inputs)
|
| 15 |
+
|
| 16 |
+
print(outputs)
|