GilbertAkham commited on
Commit
f2e1d5d
Β·
verified Β·
1 Parent(s): f6444f5

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +120 -61
app.py CHANGED
@@ -1,70 +1,129 @@
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
-
4
-
5
- def respond(
6
- message,
7
- history: list[dict[str, str]],
8
- system_message,
9
- max_tokens,
10
- temperature,
11
- top_p,
12
- hf_token: gr.OAuthToken,
13
- ):
14
- """
15
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
16
- """
17
- client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
18
-
19
- messages = [{"role": "system", "content": system_message}]
20
-
21
- messages.extend(history)
22
-
23
- messages.append({"role": "user", "content": message})
24
-
25
- response = ""
26
-
27
- for message in client.chat_completion(
28
- messages,
29
- max_tokens=max_tokens,
30
- stream=True,
31
- temperature=temperature,
32
- top_p=top_p,
33
- ):
34
- choices = message.choices
35
- token = ""
36
- if len(choices) and choices[0].delta.content:
37
- token = choices[0].delta.content
38
-
39
- response += token
40
- yield response
41
-
42
-
43
- """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  chatbot = gr.ChatInterface(
47
- respond,
48
  type="messages",
49
  additional_inputs=[
50
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
51
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
52
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
53
- gr.Slider(
54
- minimum=0.1,
55
- maximum=1.0,
56
- value=0.95,
57
- step=0.05,
58
- label="Top-p (nucleus sampling)",
59
  ),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  ],
61
  )
62
 
63
- with gr.Blocks() as demo:
64
- with gr.Sidebar():
65
- gr.LoginButton()
66
- chatbot.render()
67
-
68
-
69
  if __name__ == "__main__":
70
- demo.launch()
 
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)