Update app.py
Browse files
app.py
CHANGED
|
@@ -1,39 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
import gradio as gr
|
| 3 |
-
from
|
| 4 |
-
from
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
device = "cpu" # Force CPU usage
|
| 8 |
|
| 9 |
-
# Load the
|
| 10 |
-
|
| 11 |
-
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
return
|
| 26 |
|
| 27 |
# Create Gradio interface
|
| 28 |
-
|
| 29 |
-
fn=
|
| 30 |
-
inputs=
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
outputs=gr.Image(type="pil", label="Generated Image"), # Output generated image
|
| 35 |
-
live=True # Automatically update the output as the user changes the prompt or uploads a photo
|
| 36 |
)
|
| 37 |
|
| 38 |
-
# Launch the Gradio
|
| 39 |
-
|
|
|
|
|
|
| 1 |
+
# app.py
|
| 2 |
+
# -*- coding: utf-8 -*-
|
| 3 |
+
|
| 4 |
+
import os
|
| 5 |
import torch
|
| 6 |
import gradio as gr
|
| 7 |
+
from unsloth import FastLanguageModel
|
| 8 |
+
from trl import SFTTrainer
|
| 9 |
+
from transformers import TrainingArguments
|
| 10 |
+
from datasets import load_dataset
|
|
|
|
| 11 |
|
| 12 |
+
# Load the model and tokenizer
|
| 13 |
+
max_seq_length = 2048
|
| 14 |
+
dataset_path = "/content/dataset.jsonl" # Update this path as needed
|
| 15 |
+
dataset = load_dataset("json", data_files=dataset_path)
|
| 16 |
|
| 17 |
+
model, tokenizer = FastLanguageModel.from_pretrained(
|
| 18 |
+
model_name="unsloth/Meta-Llama-3.1-8B",
|
| 19 |
+
max_seq_length=max_seq_length,
|
| 20 |
+
dtype=None,
|
| 21 |
+
load_in_4bit=True,
|
| 22 |
+
)
|
| 23 |
|
| 24 |
+
# Prepare the model for inference
|
| 25 |
+
model = FastLanguageModel.for_inference(model)
|
| 26 |
|
| 27 |
+
# Function to generate text
|
| 28 |
+
def generate_response(user_input):
|
| 29 |
+
input = tokenizer(user_input, return_tensors="pt").to("cuda:0")
|
| 30 |
+
output = model.generate(**input)
|
| 31 |
+
return tokenizer.batch_decode(output)[0]
|
| 32 |
|
| 33 |
# Create Gradio interface
|
| 34 |
+
iface = gr.Interface(
|
| 35 |
+
fn=generate_response,
|
| 36 |
+
inputs=gr.inputs.Textbox(label="User Input"),
|
| 37 |
+
outputs=gr.outputs.Textbox(label="Bot Response"),
|
| 38 |
+
title="Chatbot with Llama 3.1",
|
| 39 |
+
description="A chatbot powered by the Llama 3.1 model. Type your message below."
|
|
|
|
|
|
|
| 40 |
)
|
| 41 |
|
| 42 |
+
# Launch the Gradio app
|
| 43 |
+
if __name__ == "__main__":
|
| 44 |
+
iface.launch()
|