Spaces:
Sleeping
Sleeping
Filip
commited on
Commit
·
8db6502
1
Parent(s):
4e33cf2
update ui
Browse files- .gitignore +1 -0
- app.py +185 -60
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
.gradio
|
app.py
CHANGED
@@ -1,64 +1,189 @@
|
|
1 |
import gradio as gr
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
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
|
6 |
-
"""
|
7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
-
|
9 |
-
|
10 |
-
def respond(
|
11 |
-
message,
|
12 |
-
history: list[tuple[str, str]],
|
13 |
-
system_message,
|
14 |
-
max_tokens,
|
15 |
-
temperature,
|
16 |
-
top_p,
|
17 |
-
):
|
18 |
-
messages = [{"role": "system", "content": system_message}]
|
19 |
-
|
20 |
-
for val in history:
|
21 |
-
if val[0]:
|
22 |
-
messages.append({"role": "user", "content": val[0]})
|
23 |
-
if val[1]:
|
24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
25 |
-
|
26 |
-
messages.append({"role": "user", "content": message})
|
27 |
-
|
28 |
-
response = ""
|
29 |
-
|
30 |
-
for message in client.chat_completion(
|
31 |
-
messages,
|
32 |
-
max_tokens=max_tokens,
|
33 |
-
stream=True,
|
34 |
-
temperature=temperature,
|
35 |
-
top_p=top_p,
|
36 |
-
):
|
37 |
-
token = message.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 |
-
demo = gr.ChatInterface(
|
47 |
-
respond,
|
48 |
-
additional_inputs=[
|
49 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
50 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
51 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
52 |
-
gr.Slider(
|
53 |
-
minimum=0.1,
|
54 |
-
maximum=1.0,
|
55 |
-
value=0.95,
|
56 |
-
step=0.05,
|
57 |
-
label="Top-p (nucleus sampling)",
|
58 |
-
),
|
59 |
-
],
|
60 |
-
)
|
61 |
|
|
|
|
|
|
|
62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
if __name__ == "__main__":
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from huggingface_hub import hf_hub_download
|
4 |
+
from llama_cpp import Llama
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
+
# Model configuration
|
7 |
+
REPO_ID = "forestav/medical_model"
|
8 |
+
MODEL_FILE = "unsloth.F16.gguf"
|
9 |
|
10 |
+
def download_model():
|
11 |
+
"""
|
12 |
+
Download the model from Hugging Face Hub if not already present
|
13 |
+
"""
|
14 |
+
try:
|
15 |
+
model_path = hf_hub_download(
|
16 |
+
repo_id=REPO_ID,
|
17 |
+
filename=MODEL_FILE,
|
18 |
+
resume_download=True,
|
19 |
+
force_filename=MODEL_FILE
|
20 |
+
)
|
21 |
+
return model_path
|
22 |
+
except Exception as e:
|
23 |
+
print(f"Error downloading model: {e}")
|
24 |
+
return None
|
25 |
+
|
26 |
+
def load_model(model_path):
|
27 |
+
"""
|
28 |
+
Load the GGUF model using llama_cpp
|
29 |
+
"""
|
30 |
+
try:
|
31 |
+
model = Llama(
|
32 |
+
model_path=model_path,
|
33 |
+
n_ctx=4096, # Adjust context window as needed
|
34 |
+
n_batch=512, # Batch size for prompt processing
|
35 |
+
verbose=False # Set to True for detailed loading info
|
36 |
+
)
|
37 |
+
return model
|
38 |
+
except Exception as e:
|
39 |
+
print(f"Error loading model: {e}")
|
40 |
+
return None
|
41 |
+
|
42 |
+
def generate_medical_response(model, prompt, max_tokens=300):
|
43 |
+
"""
|
44 |
+
Generate a medical advice response using the loaded model
|
45 |
+
"""
|
46 |
+
try:
|
47 |
+
# Prepare the prompt with a medical context
|
48 |
+
full_prompt = f"You are a helpful medical AI assistant providing professional medical advice. Respond professionally and carefully:\n\n{prompt}"
|
49 |
+
|
50 |
+
# Generate response
|
51 |
+
output = model.create_chat_completion(
|
52 |
+
messages=[
|
53 |
+
{"role": "system", "content": "You are a professional medical AI assistant."},
|
54 |
+
{"role": "user", "content": prompt}
|
55 |
+
],
|
56 |
+
max_tokens=max_tokens,
|
57 |
+
temperature=0.7,
|
58 |
+
top_p=0.9
|
59 |
+
)
|
60 |
+
|
61 |
+
# Extract and return the response text
|
62 |
+
return output['choices'][0]['message']['content']
|
63 |
+
except Exception as e:
|
64 |
+
return f"An error occurred while generating a response: {e}"
|
65 |
+
|
66 |
+
def medical_chatbot_interface(message, history):
|
67 |
+
"""
|
68 |
+
Gradio interface function for the medical chatbot
|
69 |
+
"""
|
70 |
+
# Ensure model is loaded
|
71 |
+
if not hasattr(medical_chatbot_interface, 'model'):
|
72 |
+
model_path = download_model()
|
73 |
+
if not model_path:
|
74 |
+
return "Failed to download model"
|
75 |
+
|
76 |
+
medical_chatbot_interface.model = load_model(model_path)
|
77 |
+
if not medical_chatbot_interface.model:
|
78 |
+
return "Failed to load model"
|
79 |
+
|
80 |
+
# Generate response
|
81 |
+
response = generate_medical_response(medical_chatbot_interface.model, message)
|
82 |
+
return response
|
83 |
+
|
84 |
+
# Create Gradio interface with modern, professional medical-themed UI
|
85 |
+
def create_medical_chatbot_ui():
|
86 |
+
# Modern, professional medical-themed CSS
|
87 |
+
modern_medical_css = """
|
88 |
+
:root {
|
89 |
+
--primary-color: #2c7da0;
|
90 |
+
--secondary-color: #468faf;
|
91 |
+
--background-color: #f8fbfd;
|
92 |
+
--text-color: #333;
|
93 |
+
--card-background: #ffffff;
|
94 |
+
}
|
95 |
+
|
96 |
+
body {
|
97 |
+
font-family: 'Inter', 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
98 |
+
background-color: var(--background-color);
|
99 |
+
color: var(--text-color);
|
100 |
+
line-height: 1.6;
|
101 |
+
}
|
102 |
+
|
103 |
+
.gradio-container {
|
104 |
+
background-color: var(--background-color);
|
105 |
+
max-width: 800px;
|
106 |
+
margin: 0 auto;
|
107 |
+
padding: 20px;
|
108 |
+
border-radius: 12px;
|
109 |
+
box-shadow: 0 10px 25px rgba(0, 0, 0, 0.05);
|
110 |
+
}
|
111 |
+
|
112 |
+
.chatbot-container {
|
113 |
+
background-color: var(--card-background);
|
114 |
+
border-radius: 12px;
|
115 |
+
border: 1px solid rgba(44, 125, 160, 0.1);
|
116 |
+
overflow: hidden;
|
117 |
+
}
|
118 |
+
|
119 |
+
.message-input {
|
120 |
+
border: 2px solid var(--primary-color);
|
121 |
+
border-radius: 8px;
|
122 |
+
padding: 12px;
|
123 |
+
font-size: 16px;
|
124 |
+
transition: all 0.3s ease;
|
125 |
+
}
|
126 |
+
|
127 |
+
.message-input:focus {
|
128 |
+
outline: none;
|
129 |
+
border-color: var(--secondary-color);
|
130 |
+
box-shadow: 0 0 0 3px rgba(44, 125, 160, 0.1);
|
131 |
+
}
|
132 |
+
|
133 |
+
.submit-button {
|
134 |
+
background-color: var(--primary-color);
|
135 |
+
color: white;
|
136 |
+
border: none;
|
137 |
+
border-radius: 8px;
|
138 |
+
padding: 12px 20px;
|
139 |
+
font-weight: 600;
|
140 |
+
transition: all 0.3s ease;
|
141 |
+
}
|
142 |
+
|
143 |
+
.submit-button:hover {
|
144 |
+
background-color: var(--secondary-color);
|
145 |
+
}
|
146 |
+
|
147 |
+
/* Chat message styling */
|
148 |
+
.message {
|
149 |
+
max-width: 80%;
|
150 |
+
margin: 10px 0;
|
151 |
+
padding: 12px 16px;
|
152 |
+
border-radius: 12px;
|
153 |
+
line-height: 1.5;
|
154 |
+
}
|
155 |
+
|
156 |
+
.user-message {
|
157 |
+
background-color: var(--primary-color);
|
158 |
+
color: white;
|
159 |
+
align-self: flex-end;
|
160 |
+
margin-left: auto;
|
161 |
+
}
|
162 |
+
|
163 |
+
.bot-message {
|
164 |
+
background-color: #f0f4f8;
|
165 |
+
color: var(--text-color);
|
166 |
+
align-self: flex-start;
|
167 |
+
}
|
168 |
+
"""
|
169 |
+
|
170 |
+
# Create Gradio interface with modern design
|
171 |
+
demo = gr.ChatInterface(
|
172 |
+
fn=medical_chatbot_interface,
|
173 |
+
title="🩺 MediAssist: AI Health Companion",
|
174 |
+
description="Get professional medical insights and guidance. Always consult a healthcare professional for personalized medical advice. 🌡️",
|
175 |
+
theme='soft',
|
176 |
+
css=modern_medical_css
|
177 |
+
)
|
178 |
+
|
179 |
+
return demo
|
180 |
+
|
181 |
+
# Launch the app
|
182 |
if __name__ == "__main__":
|
183 |
+
# Create and launch the Gradio app
|
184 |
+
medical_chatbot = create_medical_chatbot_ui()
|
185 |
+
medical_chatbot.launch(
|
186 |
+
server_name="0.0.0.0", # Make accessible outside the local machine
|
187 |
+
server_port=7860,
|
188 |
+
share=False # Generate a public link
|
189 |
+
)
|