Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,129 +0,0 @@
|
|
| 1 |
-
# app.py β Gilbert Multitask AI (LoRA)
|
| 2 |
-
import gradio as gr
|
| 3 |
-
import torch
|
| 4 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 5 |
-
from peft import PeftModel
|
| 6 |
-
|
| 7 |
-
# Model config
|
| 8 |
-
MODEL_NAME = "GilbertAkham/gilbert-qwen-multitask-lora"
|
| 9 |
-
BASE_MODEL = "Qwen/Qwen1.5-1.8B-Chat"
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
# ------------------------
|
| 13 |
-
# MODEL LOADING
|
| 14 |
-
# ------------------------
|
| 15 |
-
class MultitaskInference:
|
| 16 |
-
def __init__(self):
|
| 17 |
-
self.model = None
|
| 18 |
-
self.tokenizer = None
|
| 19 |
-
self.device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 20 |
-
self.load_model()
|
| 21 |
-
|
| 22 |
-
def load_model(self):
|
| 23 |
-
"""Load base + LoRA model"""
|
| 24 |
-
print("π Loading tokenizer...")
|
| 25 |
-
self.tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL, trust_remote_code=True)
|
| 26 |
-
|
| 27 |
-
print("π Loading base model...")
|
| 28 |
-
base_model = AutoModelForCausalLM.from_pretrained(
|
| 29 |
-
BASE_MODEL,
|
| 30 |
-
torch_dtype=torch.float16 if self.device == "cuda" else torch.float32,
|
| 31 |
-
device_map="auto" if self.device == "cuda" else None,
|
| 32 |
-
trust_remote_code=True,
|
| 33 |
-
)
|
| 34 |
-
|
| 35 |
-
print("π Loading LoRA adapter...")
|
| 36 |
-
self.model = PeftModel.from_pretrained(base_model, MODEL_NAME)
|
| 37 |
-
self.model.to(self.device)
|
| 38 |
-
self.model.eval()
|
| 39 |
-
print("β
Model loaded successfully!")
|
| 40 |
-
|
| 41 |
-
def generate(self, task_type, text, max_tokens=512, temperature=0.7, top_p=0.9):
|
| 42 |
-
"""Generate multitask response"""
|
| 43 |
-
task_prompts = {
|
| 44 |
-
"email": "Draft an email reply",
|
| 45 |
-
"story": "Continue the story",
|
| 46 |
-
"tech": "Answer the technical question",
|
| 47 |
-
"summary": "Summarize the content",
|
| 48 |
-
"chat": "Provide a helpful chat response"
|
| 49 |
-
}
|
| 50 |
-
|
| 51 |
-
prompt = f"### Task: {task_prompts[task_type]}\n\n### Input:\n{text}\n\n### Output:\n"
|
| 52 |
-
try:
|
| 53 |
-
inputs = self.tokenizer(prompt, return_tensors="pt", truncation=True, max_length=1024).to(self.device)
|
| 54 |
-
|
| 55 |
-
with torch.no_grad():
|
| 56 |
-
outputs = self.model.generate(
|
| 57 |
-
**inputs,
|
| 58 |
-
max_new_tokens=max_tokens,
|
| 59 |
-
temperature=temperature,
|
| 60 |
-
do_sample=True,
|
| 61 |
-
top_p=top_p,
|
| 62 |
-
repetition_penalty=1.1,
|
| 63 |
-
pad_token_id=self.tokenizer.eos_token_id,
|
| 64 |
-
)
|
| 65 |
-
|
| 66 |
-
response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 67 |
-
if "### Output:" in response:
|
| 68 |
-
response = response.split("### Output:")[-1].strip()
|
| 69 |
-
return response
|
| 70 |
-
|
| 71 |
-
except Exception as e:
|
| 72 |
-
return f"β Error generating response: {e}"
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
# ------------------------
|
| 76 |
-
# GRADIO INTERFACE
|
| 77 |
-
# ------------------------
|
| 78 |
-
engine = MultitaskInference()
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
def chat_response(message, history, task_type, max_tokens, temperature, top_p):
|
| 82 |
-
"""Chat handler for Gradio ChatInterface"""
|
| 83 |
-
try:
|
| 84 |
-
reply = engine.generate(
|
| 85 |
-
task_type=task_type,
|
| 86 |
-
text=message,
|
| 87 |
-
max_tokens=max_tokens,
|
| 88 |
-
temperature=temperature,
|
| 89 |
-
top_p=top_p
|
| 90 |
-
)
|
| 91 |
-
yield reply
|
| 92 |
-
except Exception as e:
|
| 93 |
-
yield f"β Error: {e}"
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
# ------------------------
|
| 97 |
-
# BUILD CHAT APP
|
| 98 |
-
# ------------------------
|
| 99 |
-
chatbot = gr.ChatInterface(
|
| 100 |
-
fn=chat_response,
|
| 101 |
-
type="messages",
|
| 102 |
-
additional_inputs=[
|
| 103 |
-
gr.Dropdown(
|
| 104 |
-
choices=["chat", "email", "story", "tech", "summary"],
|
| 105 |
-
value="chat",
|
| 106 |
-
label="π― Task Type",
|
| 107 |
-
info="Select the text generation mode",
|
| 108 |
-
),
|
| 109 |
-
gr.Slider(minimum=64, maximum=1024, value=512, step=32, label="π Max new tokens"),
|
| 110 |
-
gr.Slider(minimum=0.1, maximum=1.0, value=0.7, step=0.05, label="π‘οΈ Temperature"),
|
| 111 |
-
gr.Slider(minimum=0.1, maximum=1.0, value=0.9, step=0.05, label="π² Top-p"),
|
| 112 |
-
],
|
| 113 |
-
title="π Gilbert Multitask AI",
|
| 114 |
-
description=(
|
| 115 |
-
"**Base Model:** Qwen1.5-1.8B-Chat\n\n"
|
| 116 |
-
"LoRA fine-tuned for: Email drafting, story continuation, tech Q&A, summarization, and chat responses."
|
| 117 |
-
),
|
| 118 |
-
theme=gr.themes.Soft(),
|
| 119 |
-
examples=[
|
| 120 |
-
["Write a professional email update for a client about completing a milestone."],
|
| 121 |
-
["Continue the story: The spaceship hummed as Captain Lira adjusted the controls..."],
|
| 122 |
-
["How can I fix a 'ModuleNotFoundError' in Python?"],
|
| 123 |
-
["Summarize: Artificial intelligence is transforming industries through automation and insights."],
|
| 124 |
-
["Hey, I canβt access the company VPN. What should I do?"],
|
| 125 |
-
],
|
| 126 |
-
)
|
| 127 |
-
|
| 128 |
-
if __name__ == "__main__":
|
| 129 |
-
chatbot.launch(server_name="0.0.0.0", server_port=7860, share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|