Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
|
| 3 |
+
# Load your model and tokenizer
|
| 4 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 5 |
+
|
| 6 |
+
model_name = "ahmedbasemdev/LLama3.2-fine-tuned" # Replace with your model name
|
| 7 |
+
model = AutoModelForCausalLM.from_pretrained(model_name)
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
+
|
| 10 |
+
def single_inference(question):
|
| 11 |
+
messages = []
|
| 12 |
+
|
| 13 |
+
messages.append({"role": "user", "content": question})
|
| 14 |
+
|
| 15 |
+
input_ids = tokenizer.apply_chat_template(
|
| 16 |
+
messages,
|
| 17 |
+
add_generation_prompt=True,
|
| 18 |
+
return_tensors="pt"
|
| 19 |
+
).to(model.device)
|
| 20 |
+
|
| 21 |
+
terminators = [
|
| 22 |
+
tokenizer.eos_token_id,
|
| 23 |
+
tokenizer.convert_tokens_to_ids("<|eot_id|>")
|
| 24 |
+
]
|
| 25 |
+
|
| 26 |
+
outputs = model.generate(
|
| 27 |
+
input_ids,
|
| 28 |
+
max_new_tokens=256,
|
| 29 |
+
eos_token_id=terminators,
|
| 30 |
+
do_sample=True,
|
| 31 |
+
temperature=0.2,
|
| 32 |
+
)
|
| 33 |
+
response = outputs[0][input_ids.shape[-1]:]
|
| 34 |
+
output = tokenizer.decode(response, skip_special_tokens=True)
|
| 35 |
+
return output
|
| 36 |
+
|
| 37 |
+
# Create the Gradio interface
|
| 38 |
+
interface = gr.Interface(
|
| 39 |
+
fn=single_inference, # Function to wrap
|
| 40 |
+
inputs=gr.Textbox(lines=2, placeholder="Ask a question..."), # Input type
|
| 41 |
+
outputs=gr.Textbox(label="Response"), # Output type
|
| 42 |
+
title="Chat with Your Model", # App title
|
| 43 |
+
description="Enter a question, and the model will generate a response.", # App description
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
# Launch the app
|
| 47 |
+
if __name__ == "__main__":
|
| 48 |
+
interface.launch()
|