Bhaskar2611 commited on
Commit
8347b4f
·
verified ·
1 Parent(s): a27ca0e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +220 -86
app.py CHANGED
@@ -1,96 +1,230 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
3
 
4
- # Initialize the client with your desired model
5
- client = InferenceClient("Bhaskar2611/Capstone")
6
 
7
- # Define the system prompt as an AI Dermatologist
 
 
 
 
 
 
 
 
 
 
8
  def format_prompt(message, history):
9
- prompt = "<s>"
10
- # Start the conversation with a system message
11
- prompt += "[INST] You are an AI Dermatologist chatbot designed to assist users with skin by only providing text and if user information is not provided related to skin then ask what they want to know related to skin.[/INST]"
12
- for user_prompt, bot_response in history:
13
- prompt += f"[INST] {user_prompt} [/INST]"
14
- prompt += f" {bot_response}</s> "
15
- prompt += f"[INST] {message} [/INST]"
 
 
16
  return prompt
17
 
18
- # Function to generate responses with the AI Dermatologist context
19
- def generate(
20
- prompt, history, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0
21
- ):
22
- temperature = float(temperature)
23
- if temperature < 1e-2:
24
- temperature = 1e-2
25
- top_p = float(top_p)
26
-
27
- generate_kwargs = dict(
28
- temperature=temperature,
29
- max_new_tokens=max_new_tokens,
30
- top_p=top_p,
31
- repetition_penalty=repetition_penalty,
32
  do_sample=True,
33
- seed=42,
 
 
34
  )
 
 
 
 
35
 
36
- formatted_prompt = format_prompt(prompt, history)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
- stream = client.text_generation(
39
- formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False
40
- )
41
- output = ""
42
-
43
- for response in stream:
44
- output += response.token.text
45
- yield output
46
- return output
47
-
48
- # Customizable input controls for the chatbot interface
49
- Settings = [
50
- gr.Slider(
51
- label="Temperature",
52
- value=0.9,
53
- minimum=0.0,
54
- maximum=1.0,
55
- step=0.05,
56
- interactive=True,
57
- info="Higher values produce more diverse outputs",
58
- ),
59
- gr.Slider(
60
- label="Max new tokens",
61
- value=256,
62
- minimum=0,
63
- maximum=1048,
64
- step=64,
65
- interactive=True,
66
- info="The maximum numbers of new tokens",
67
- ),
68
- gr.Slider(
69
- label="Top-p (nucleus sampling)",
70
- value=0.90,
71
- minimum=0.0,
72
- maximum=1,
73
- step=0.05,
74
- interactive=True,
75
- info="Higher values sample more low-probability tokens",
76
- ),
77
- gr.Slider(
78
- label="Repetition penalty",
79
- value=1.2,
80
- minimum=1.0,
81
- maximum=2.0,
82
- step=0.05,
83
- interactive=True,
84
- info="Penalize repeated tokens",
85
- )
86
- ]
87
- # Define the chatbot interface with the starting system message as AI Dermatologist
88
- gr.ChatInterface(
89
- fn=generate,
90
- chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, layout="panel"),
91
- additional_inputs = Settings,
92
- title="Skin Bot"
93
- ).launch(show_api=False)
94
-
95
- # Load your model after launching the interface
96
- # gr.load("models/Bhaskar2611/Capstone").launch()
 
1
+ # import gradio as gr
2
+ # from huggingface_hub import InferenceClient
3
+
4
+ # # Initialize the client with your desired model
5
+ # client = InferenceClient("Bhaskar2611/Capstone")
6
+
7
+ # # Define the system prompt as an AI Dermatologist
8
+ # def format_prompt(message, history):
9
+ # prompt = "<s>"
10
+ # # Start the conversation with a system message
11
+ # prompt += "[INST] You are an AI Dermatologist chatbot designed to assist users with skin by only providing text and if user information is not provided related to skin then ask what they want to know related to skin.[/INST]"
12
+ # for user_prompt, bot_response in history:
13
+ # prompt += f"[INST] {user_prompt} [/INST]"
14
+ # prompt += f" {bot_response}</s> "
15
+ # prompt += f"[INST] {message} [/INST]"
16
+ # return prompt
17
+
18
+ # # Function to generate responses with the AI Dermatologist context
19
+ # def generate(
20
+ # prompt, history, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0
21
+ # ):
22
+ # temperature = float(temperature)
23
+ # if temperature < 1e-2:
24
+ # temperature = 1e-2
25
+ # top_p = float(top_p)
26
+
27
+ # generate_kwargs = dict(
28
+ # temperature=temperature,
29
+ # max_new_tokens=max_new_tokens,
30
+ # top_p=top_p,
31
+ # repetition_penalty=repetition_penalty,
32
+ # do_sample=True,
33
+ # seed=42,
34
+ # )
35
+
36
+ # formatted_prompt = format_prompt(prompt, history)
37
+
38
+ # stream = client.text_generation(
39
+ # formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False
40
+ # )
41
+ # output = ""
42
+
43
+ # for response in stream:
44
+ # output += response.token.text
45
+ # yield output
46
+ # return output
47
+
48
+ # # Customizable input controls for the chatbot interface
49
+ # Settings = [
50
+ # gr.Slider(
51
+ # label="Temperature",
52
+ # value=0.9,
53
+ # minimum=0.0,
54
+ # maximum=1.0,
55
+ # step=0.05,
56
+ # interactive=True,
57
+ # info="Higher values produce more diverse outputs",
58
+ # ),
59
+ # gr.Slider(
60
+ # label="Max new tokens",
61
+ # value=256,
62
+ # minimum=0,
63
+ # maximum=1048,
64
+ # step=64,
65
+ # interactive=True,
66
+ # info="The maximum numbers of new tokens",
67
+ # ),
68
+ # gr.Slider(
69
+ # label="Top-p (nucleus sampling)",
70
+ # value=0.90,
71
+ # minimum=0.0,
72
+ # maximum=1,
73
+ # step=0.05,
74
+ # interactive=True,
75
+ # info="Higher values sample more low-probability tokens",
76
+ # ),
77
+ # gr.Slider(
78
+ # label="Repetition penalty",
79
+ # value=1.2,
80
+ # minimum=1.0,
81
+ # maximum=2.0,
82
+ # step=0.05,
83
+ # interactive=True,
84
+ # info="Penalize repeated tokens",
85
+ # )
86
+ # ]
87
+ # # Define the chatbot interface with the starting system message as AI Dermatologist
88
+ # gr.ChatInterface(
89
+ # fn=generate,
90
+ # chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, layout="panel"),
91
+ # additional_inputs = Settings,
92
+ # title="Skin Bot"
93
+ # ).launch(show_api=False)
94
+
95
+ # # Load your model after launching the interface
96
+ # # gr.load("models/Bhaskar2611/Capstone").launch()
97
+
98
+ # import gradio as gr
99
+ # from huggingface_hub import InferenceClient
100
+
101
+ # # Initialize the client with your Hugging Face token
102
+ # client = InferenceClient(
103
+ # model="HuggingFaceH4/zephyr-7b-beta",
104
+ # hf_token = os.getenv("HF_TOKEN")
105
+ # )
106
+
107
+ # # Define the system prompt as an AI Dermatologist
108
+ # def format_prompt(message, history):
109
+ # prompt = "<s>"
110
+ # prompt += "[INST] You are an AI Dermatologist chatbot designed to assist users with skin by only providing text and if user information is not provided related to skin then ask what they want to know related to skin.[/INST]"
111
+ # for user_prompt, bot_response in history:
112
+ # prompt += f"[INST] {user_prompt} [/INST] {bot_response}</s> "
113
+ # prompt += f"[INST] {message} [/INST]"
114
+ # return prompt
115
+
116
+ # # Function to generate responses
117
+ # def generate(prompt, history, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0):
118
+ # temperature = float(temperature)
119
+ # if temperature < 1e-2:
120
+ # temperature = 1e-2
121
+ # top_p = float(top_p)
122
+
123
+ # generate_kwargs = dict(
124
+ # temperature=temperature,
125
+ # max_new_tokens=max_new_tokens,
126
+ # top_p=top_p,
127
+ # repetition_penalty=repetition_penalty,
128
+ # do_sample=True,
129
+ # seed=42,
130
+ # )
131
+
132
+ # formatted_prompt = format_prompt(prompt, history)
133
+
134
+ # stream = client.text_generation(
135
+ # formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False
136
+ # )
137
+
138
+ # output = ""
139
+ # for response in stream:
140
+ # output += response.token.text
141
+ # yield output
142
+ # return output
143
+
144
+ # # Sliders for customization
145
+ # Settings = [
146
+ # gr.Slider(label="Temperature", value=0.9, minimum=0.0, maximum=1.0, step=0.05, interactive=True, info="Higher values produce more diverse outputs"),
147
+ # gr.Slider(label="Max new tokens", value=256, minimum=0, maximum=1048, step=64, interactive=True, info="The maximum number of new tokens"),
148
+ # gr.Slider(label="Top-p (nucleus sampling)", value=0.90, minimum=0.0, maximum=1, step=0.05, interactive=True, info="Higher values sample more low-probability tokens"),
149
+ # gr.Slider(label="Repetition penalty", value=1.2, minimum=1.0, maximum=2.0, step=0.05, interactive=True, info="Penalize repeated tokens")
150
+ # ]
151
+
152
+ # # Chat interface
153
+ # gr.ChatInterface(
154
+ # fn=generate,
155
+ # chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, layout="panel"),
156
+ # additional_inputs=Settings,
157
+ # title="Skin Bot"
158
+ # ).launch(show_api=False)
159
+
160
+ # # Load any additional models if needed
161
+ # # gr.load("models/Bhaskar2611/Capstone").launch()
162
+
163
+ import torch
164
+ import transformers
165
+ from transformers import AutoTokenizer, AutoModelForCausalLM, TextStreamer
166
  import gradio as gr
 
167
 
168
+ # Load tokenizer and model from Hugging Face (your fine-tuned model)
169
+ model_id = "Bhaskar2611/Capstone"
170
 
171
+ tokenizer = AutoTokenizer.from_pretrained(model_id, use_auth_token=True)
172
+ model = AutoModelForCausalLM.from_pretrained(
173
+ model_id,
174
+ torch_dtype=torch.float16,
175
+ device_map="auto",
176
+ use_auth_token=True
177
+ )
178
+
179
+ streamer = TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
180
+
181
+ # Function to format prompt with Zephyr-style and system prompt
182
  def format_prompt(message, history):
183
+ system_prompt = (
184
+ "You are a helpful medical assistant specialized in skin diseases. "
185
+ "Always provide accurate, responsible, and actionable information. "
186
+ "If needed, recommend seeing a dermatologist."
187
+ )
188
+ prompt = f"<|user|>\n{system_prompt}\n<|assistant|>\nSure, I'm here to help you.</s>\n"
189
+ for user_msg, bot_msg in history:
190
+ prompt += f"<|user|>\n{user_msg}\n<|assistant|>\n{bot_msg}</s>\n"
191
+ prompt += f"<|user|>\n{message}\n<|assistant|>\n"
192
  return prompt
193
 
194
+ # Function to generate model response
195
+ def respond(message, history):
196
+ prompt = format_prompt(message, history)
197
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
198
+ output = model.generate(
199
+ **inputs,
200
+ max_new_tokens=2048,
201
+ pad_token_id=tokenizer.eos_token_id,
 
 
 
 
 
 
202
  do_sample=True,
203
+ temperature=0.7,
204
+ top_p=0.9,
205
+ streamer=streamer
206
  )
207
+ decoded_output = tokenizer.decode(output[0], skip_special_tokens=True)
208
+ # Extract the final assistant message only
209
+ final_response = decoded_output.split("<|assistant|>")[-1].strip().split("</s>")[0].strip()
210
+ return final_response
211
 
212
+ # Create Gradio interface
213
+ chat = gr.ChatInterface(
214
+ fn=respond,
215
+ title="Skin & Hair Disease Assistant",
216
+ chatbot=gr.Chatbot(height=400),
217
+ textbox=gr.Textbox(placeholder="Describe your symptoms or ask a question...", container=False, scale=7),
218
+ description="Ask about skin conditions, hair issues, or treatment guidance. Powered by a custom fine-tuned Zephyr model.",
219
+ theme="soft",
220
+ examples=["I have red patches on my skin.", "What causes hair loss?", "Is dandruff a fungal infection?"],
221
+ cache_examples=False,
222
+ retry_btn="🔁 Retry",
223
+ undo_btn="↩️ Undo",
224
+ clear_btn="🧹 Clear"
225
+ )
226
+
227
+ # Launch app
228
+ if __name__ == "__main__":
229
+ chat.launch()
230