Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -160,61 +160,115 @@
|
|
| 160 |
# # Load any additional models if needed
|
| 161 |
# # gr.load("models/Bhaskar2611/Capstone").launch()
|
| 162 |
|
| 163 |
-
import os
|
| 164 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
| 165 |
-
import gradio as gr
|
| 166 |
|
| 167 |
-
# Load your Hugging Face token (if needed for private models or API limit increases)
|
| 168 |
-
hf_token = os.environ.get("HF_TOKEN")
|
| 169 |
|
| 170 |
-
# Model ID for Mistral 7B Instruct
|
| 171 |
-
model_id = "mistralai/Mistral-7B-Instruct-v0.1"
|
| 172 |
|
| 173 |
-
# Load tokenizer
|
| 174 |
-
tokenizer = AutoTokenizer.from_pretrained(model_id, token=hf_token)
|
| 175 |
|
| 176 |
-
# BitsAndBytesConfig for 4-bit quantization to reduce memory usage
|
| 177 |
-
bnb_config = BitsAndBytesConfig(load_in_4bit=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
|
| 179 |
-
#
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
)
|
| 186 |
|
| 187 |
-
# Skin assistant
|
| 188 |
SKIN_ASSISTANT_PROMPT = (
|
| 189 |
-
"You are
|
| 190 |
-
"
|
| 191 |
-
"treatments, and care. Always respond in a clear and empathetic way.\n\n"
|
| 192 |
)
|
| 193 |
|
| 194 |
-
def
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
temperature=0.7,
|
| 202 |
top_p=0.95,
|
| 203 |
-
|
| 204 |
-
)
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
description="Ask any questions related to skin diseases and get expert-like responses."
|
| 215 |
)
|
| 216 |
|
| 217 |
if __name__ == "__main__":
|
| 218 |
-
|
|
|
|
| 219 |
|
| 220 |
|
|
|
|
| 160 |
# # Load any additional models if needed
|
| 161 |
# # gr.load("models/Bhaskar2611/Capstone").launch()
|
| 162 |
|
| 163 |
+
# import os
|
| 164 |
+
# from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
| 165 |
+
# import gradio as gr
|
| 166 |
|
| 167 |
+
# # Load your Hugging Face token (if needed for private models or API limit increases)
|
| 168 |
+
# hf_token = os.environ.get("HF_TOKEN")
|
| 169 |
|
| 170 |
+
# # Model ID for Mistral 7B Instruct
|
| 171 |
+
# model_id = "mistralai/Mistral-7B-Instruct-v0.1"
|
| 172 |
|
| 173 |
+
# # Load tokenizer
|
| 174 |
+
# tokenizer = AutoTokenizer.from_pretrained(model_id, token=hf_token)
|
| 175 |
|
| 176 |
+
# # BitsAndBytesConfig for 4-bit quantization to reduce memory usage
|
| 177 |
+
# bnb_config = BitsAndBytesConfig(load_in_4bit=True)
|
| 178 |
+
|
| 179 |
+
# # Load model with quantization and device mapping
|
| 180 |
+
# model = AutoModelForCausalLM.from_pretrained(
|
| 181 |
+
# model_id,
|
| 182 |
+
# quantization_config=bnb_config,
|
| 183 |
+
# device_map="auto",
|
| 184 |
+
# token=hf_token
|
| 185 |
+
# )
|
| 186 |
|
| 187 |
+
# # Skin assistant system prompt
|
| 188 |
+
# SKIN_ASSISTANT_PROMPT = (
|
| 189 |
+
# "You are a helpful assistant specialized in skin diseases and dermatology. "
|
| 190 |
+
# "Provide accurate, concise, and helpful advice about skin conditions, symptoms, "
|
| 191 |
+
# "treatments, and care. Always respond in a clear and empathetic way.\n\n"
|
| 192 |
+
# )
|
| 193 |
+
|
| 194 |
+
# def generate_response(user_input):
|
| 195 |
+
# prompt = SKIN_ASSISTANT_PROMPT + user_input
|
| 196 |
+
# inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 197 |
+
# outputs = model.generate(
|
| 198 |
+
# **inputs,
|
| 199 |
+
# max_new_tokens=1024,
|
| 200 |
+
# do_sample=True,
|
| 201 |
+
# temperature=0.7,
|
| 202 |
+
# top_p=0.95,
|
| 203 |
+
# repetition_penalty=1.1
|
| 204 |
+
# )
|
| 205 |
+
# response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 206 |
+
# return response.replace(SKIN_ASSISTANT_PROMPT, "").strip()
|
| 207 |
+
|
| 208 |
+
# # Gradio interface
|
| 209 |
+
# iface = gr.Interface(
|
| 210 |
+
# fn=generate_response,
|
| 211 |
+
# inputs=gr.Textbox(lines=3, placeholder="Ask about skin diseases..."),
|
| 212 |
+
# outputs="text",
|
| 213 |
+
# title="Skin Disease Assistant",
|
| 214 |
+
# description="Ask any questions related to skin diseases and get expert-like responses."
|
| 215 |
+
# )
|
| 216 |
+
|
| 217 |
+
# if __name__ == "__main__":
|
| 218 |
+
# iface.launch()
|
| 219 |
+
|
| 220 |
+
import os
|
| 221 |
+
import gradio as gr
|
| 222 |
+
from huggingface_hub import InferenceClient
|
| 223 |
+
from dotenv import load_dotenv
|
| 224 |
+
|
| 225 |
+
# Load API token from .env or environment
|
| 226 |
+
load_dotenv()
|
| 227 |
+
HF_TOKEN = os.getenv("HF_TOKEN") # or directly use your token here
|
| 228 |
+
|
| 229 |
+
# Initialize the Hugging Face inference client
|
| 230 |
+
client = InferenceClient(
|
| 231 |
+
model="mistralai/Mistral-7B-Instruct-v0.3",
|
| 232 |
+
token=HF_TOKEN
|
| 233 |
)
|
| 234 |
|
| 235 |
+
# Skin assistant prompt
|
| 236 |
SKIN_ASSISTANT_PROMPT = (
|
| 237 |
+
"You are an AI Dermatologist chatbot designed to assist users with skin by only providing text "
|
| 238 |
+
"and if user information is not provided related to skin then ask what they want to know related to skin."
|
|
|
|
| 239 |
)
|
| 240 |
|
| 241 |
+
def respond(message, history):
|
| 242 |
+
messages = [{"role": "system", "content": SKIN_ASSISTANT_PROMPT}]
|
| 243 |
+
for user_msg, bot_msg in history:
|
| 244 |
+
if user_msg:
|
| 245 |
+
messages.append({"role": "user", "content": user_msg})
|
| 246 |
+
if bot_msg:
|
| 247 |
+
messages.append({"role": "assistant", "content": bot_msg})
|
| 248 |
+
messages.append({"role": "user", "content": message})
|
| 249 |
+
|
| 250 |
+
response = ""
|
| 251 |
+
for chunk in client.chat.completions.create(
|
| 252 |
+
model="mistralai/Mistral-7B-Instruct-v0.3",
|
| 253 |
+
messages=messages,
|
| 254 |
+
max_tokens=1024,
|
| 255 |
temperature=0.7,
|
| 256 |
top_p=0.95,
|
| 257 |
+
stream=True,
|
| 258 |
+
):
|
| 259 |
+
token = chunk.choices[0].delta.get("content", "")
|
| 260 |
+
response += token
|
| 261 |
+
yield response
|
| 262 |
+
|
| 263 |
+
# Launch Gradio interface
|
| 264 |
+
demo = gr.ChatInterface(
|
| 265 |
+
fn=respond,
|
| 266 |
+
title="skin-bot",
|
| 267 |
+
theme="default"
|
|
|
|
| 268 |
)
|
| 269 |
|
| 270 |
if __name__ == "__main__":
|
| 271 |
+
demo.launch()
|
| 272 |
+
|
| 273 |
|
| 274 |
|