Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
2 |
+
from peft import PeftModel
|
3 |
+
import torch
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
# --- Model Loading ---
|
7 |
+
base_model_id = "meta-llama/Meta-Llama-3-8B"
|
8 |
+
lora_model_id = "Nlpeva/lora_model" # Replace with your LoRA Hub path
|
9 |
+
|
10 |
+
try:
|
11 |
+
model = AutoModelForCausalLM.from_pretrained(
|
12 |
+
base_model_id,
|
13 |
+
torch_dtype=torch.float16,
|
14 |
+
device_map="auto"
|
15 |
+
)
|
16 |
+
tokenizer = AutoTokenizer.from_pretrained(base_model_id)
|
17 |
+
model = PeftModel.from_pretrained(model, lora_model_id)
|
18 |
+
print("Model and LoRA loaded successfully!")
|
19 |
+
except Exception as e:
|
20 |
+
print(f"Error loading model or LoRA: {e}")
|
21 |
+
model = None
|
22 |
+
tokenizer = None
|
23 |
+
|
24 |
+
# --- Generation Function ---
|
25 |
+
def generate_response(information, input_text):
|
26 |
+
if model is None or tokenizer is None:
|
27 |
+
return "Model not loaded. Please check the logs."
|
28 |
+
|
29 |
+
prompt = f"Information: {information}\n\nInput: {input_text}\n\nResponse:"
|
30 |
+
input_ids = tokenizer.encode(prompt, return_tensors="pt").to(model.device)
|
31 |
+
|
32 |
+
try:
|
33 |
+
with torch.no_grad():
|
34 |
+
output = model.generate(
|
35 |
+
input_ids=input_ids,
|
36 |
+
max_length=300, # Adjust as needed
|
37 |
+
num_return_sequences=1,
|
38 |
+
temperature=0.7,
|
39 |
+
top_p=0.9,
|
40 |
+
# Add other generation parameters as desired
|
41 |
+
)
|
42 |
+
generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
43 |
+
return generated_text.strip()
|
44 |
+
except Exception as e:
|
45 |
+
return f"Error during generation: {e}"
|
46 |
+
|
47 |
+
# --- Gradio Interface ---
|
48 |
+
iface = gr.Interface(
|
49 |
+
fn=generate_response,
|
50 |
+
inputs=[
|
51 |
+
gr.Textbox(label="Information", placeholder="Provide any relevant context or information here."),
|
52 |
+
gr.Textbox(label="Input", placeholder="Enter your query or the text you want the model to process.")
|
53 |
+
],
|
54 |
+
outputs=gr.Textbox(label="Output"),
|
55 |
+
title="Llama-3 with Custom LoRA",
|
56 |
+
description="Enter information and an input, and the model will generate a response based on both."
|
57 |
+
)
|
58 |
+
|
59 |
+
iface.launch()
|