Update app.py
Browse files
app.py
CHANGED
@@ -1,70 +1,70 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
|
9 |
-
|
10 |
-
|
11 |
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
|
29 |
-
|
30 |
|
31 |
-
|
32 |
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
|
42 |
-
|
43 |
-
|
44 |
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
|
65 |
|
66 |
-
|
67 |
-
|
68 |
|
69 |
|
70 |
# import gradio as gr
|
@@ -170,70 +170,70 @@
|
|
170 |
# iface.launch()
|
171 |
|
172 |
|
173 |
-
import gradio as gr
|
174 |
-
from langdetect import detect
|
175 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM, TextGenerationPipeline, GenerationConfig
|
176 |
-
import torch
|
177 |
|
178 |
-
# Load model and tokenizer
|
179 |
-
model_name = "FreedomIntelligence/Apollo-7B"
|
180 |
|
181 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
182 |
-
model = AutoModelForCausalLM.from_pretrained(model_name)
|
183 |
|
184 |
-
tokenizer.pad_token = tokenizer.eos_token
|
185 |
|
186 |
-
generation_config = GenerationConfig(
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
)
|
194 |
|
195 |
-
# Create generation pipeline
|
196 |
-
pipe = TextGenerationPipeline(
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
)
|
201 |
|
202 |
-
# Prompt formatter based on language
|
203 |
-
def generate_prompt(message, history):
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
- عدم تكرار أي نقطة أو عبارة أو كلمة
|
209 |
-
- وضوح وسلاسة كل نقطة
|
210 |
-
- تجنب الحشو والعبارات الزائدة
|
211 |
-
|
212 |
-
السؤال: {message}
|
213 |
-
الإجابة:"""
|
214 |
-
else:
|
215 |
-
return f"""Answer the following medical question in clear English with a detailed, non-redundant response. Do not repeat ideas or restate the question. If information is missing, rely on your prior medical knowledge:
|
216 |
-
Question: {message}
|
217 |
-
Answer:"""
|
218 |
-
|
219 |
-
# Chat function
|
220 |
-
def chat_fn(message, history):
|
221 |
-
prompt = generate_prompt(message, history)
|
222 |
-
response = pipe(prompt,
|
223 |
-
max_new_tokens=512,
|
224 |
-
temperature=0.7,
|
225 |
-
do_sample = True,
|
226 |
-
top_p=0.9)[0]['generated_text']
|
227 |
-
answer = response.split("Answer:")[-1].strip() if "Answer:" in response else response.split("الإجابة:")[-1].strip()
|
228 |
-
return answer
|
229 |
-
|
230 |
-
# Gradio ChatInterface
|
231 |
-
demo = gr.ChatInterface(
|
232 |
-
fn=chat_fn,
|
233 |
-
title="🩺 Apollo-7B Medical Chatbot",
|
234 |
-
description="Multilingual (Arabic & English) medical Q&A chatbot powered by Apollo-7B. No RAG, just fast model inference.",
|
235 |
-
theme=gr.themes.Soft()
|
236 |
-
)
|
237 |
|
238 |
-
|
239 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
+
import os
|
4 |
|
5 |
+
"""
|
6 |
+
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
|
7 |
+
"""
|
8 |
|
9 |
+
token = os.getenv("HF_TOKEN")
|
10 |
+
client = InferenceClient("emilyalsentzer/Bio_ClinicalBERT", token=token)
|
11 |
|
12 |
|
13 |
+
def respond(
|
14 |
+
message,
|
15 |
+
history: list[tuple[str, str]],
|
16 |
+
system_message,
|
17 |
+
max_tokens,
|
18 |
+
temperature,
|
19 |
+
top_p,
|
20 |
+
):
|
21 |
+
messages = [{"role": "system", "content": system_message}]
|
22 |
|
23 |
+
for val in history:
|
24 |
+
if val[0]:
|
25 |
+
messages.append({"role": "user", "content": val[0]})
|
26 |
+
if val[1]:
|
27 |
+
messages.append({"role": "assistant", "content": val[1]})
|
28 |
|
29 |
+
messages.append({"role": "user", "content": message})
|
30 |
|
31 |
+
response = ""
|
32 |
|
33 |
+
for message in client.chat_completion(
|
34 |
+
messages,
|
35 |
+
max_tokens=max_tokens,
|
36 |
+
stream=True,
|
37 |
+
temperature=temperature,
|
38 |
+
top_p=top_p,
|
39 |
+
):
|
40 |
+
token = message.choices[0].delta.content
|
41 |
|
42 |
+
response += token
|
43 |
+
yield response
|
44 |
|
45 |
|
46 |
+
"""
|
47 |
+
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
48 |
+
"""
|
49 |
+
demo = gr.ChatInterface(
|
50 |
+
respond,
|
51 |
+
additional_inputs=[
|
52 |
+
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
53 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
54 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
55 |
+
gr.Slider(
|
56 |
+
minimum=0.1,
|
57 |
+
maximum=1.0,
|
58 |
+
value=0.95,
|
59 |
+
step=0.05,
|
60 |
+
label="Top-p (nucleus sampling)",
|
61 |
+
),
|
62 |
+
],
|
63 |
+
)
|
64 |
|
65 |
|
66 |
+
if __name__ == "__main__":
|
67 |
+
demo.launch()
|
68 |
|
69 |
|
70 |
# import gradio as gr
|
|
|
170 |
# iface.launch()
|
171 |
|
172 |
|
173 |
+
# import gradio as gr
|
174 |
+
# from langdetect import detect
|
175 |
+
# from transformers import AutoTokenizer, AutoModelForCausalLM, TextGenerationPipeline, GenerationConfig
|
176 |
+
# import torch
|
177 |
|
178 |
+
# # Load model and tokenizer
|
179 |
+
# model_name = "FreedomIntelligence/Apollo-7B"
|
180 |
|
181 |
+
# tokenizer = AutoTokenizer.from_pretrained(model_name)
|
182 |
+
# model = AutoModelForCausalLM.from_pretrained(model_name)
|
183 |
|
184 |
+
# tokenizer.pad_token = tokenizer.eos_token
|
185 |
|
186 |
+
# generation_config = GenerationConfig(
|
187 |
+
# max_new_tokens=150,
|
188 |
+
# temperature=0.2,
|
189 |
+
# top_k=20,
|
190 |
+
# do_sample=True,
|
191 |
+
# top_p=0.7,
|
192 |
+
# repetition_penalty=1.3,
|
193 |
+
# )
|
194 |
|
195 |
+
# # Create generation pipeline
|
196 |
+
# pipe = TextGenerationPipeline(
|
197 |
+
# model=model,
|
198 |
+
# tokenizer=tokenizer,
|
199 |
+
# device=model.device.index if torch.cuda.is_available() else "cpu"
|
200 |
+
# )
|
201 |
|
202 |
+
# # Prompt formatter based on language
|
203 |
+
# def generate_prompt(message, history):
|
204 |
+
# lang = detect(message)
|
205 |
+
# if lang == "ar":
|
206 |
+
# return f"""أجب على السؤال الطبي التالي بلغة عربية فصحى، بإجابة دقيقة ومفصلة. إذا لم تجد معلومات كافية في السياق، استخدم معرفتك الطبية السابقة.
|
207 |
+
# وتأكد من ان:
|
208 |
+
# - عدم تكرار أي نقطة أو عبارة أو كلمة
|
209 |
+
# - وضوح وسلاسة كل نقطة
|
210 |
+
# - تجنب الحشو والعبارات الزائدة
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
211 |
|
212 |
+
# السؤال: {message}
|
213 |
+
# الإجابة:"""
|
214 |
+
# else:
|
215 |
+
# return f"""Answer the following medical question in clear English with a detailed, non-redundant response. Do not repeat ideas or restate the question. If information is missing, rely on your prior medical knowledge:
|
216 |
+
# Question: {message}
|
217 |
+
# Answer:"""
|
218 |
+
|
219 |
+
# # Chat function
|
220 |
+
# def chat_fn(message, history):
|
221 |
+
# prompt = generate_prompt(message, history)
|
222 |
+
# response = pipe(prompt,
|
223 |
+
# max_new_tokens=512,
|
224 |
+
# temperature=0.7,
|
225 |
+
# do_sample = True,
|
226 |
+
# top_p=0.9)[0]['generated_text']
|
227 |
+
# answer = response.split("Answer:")[-1].strip() if "Answer:" in response else response.split("الإجابة:")[-1].strip()
|
228 |
+
# return answer
|
229 |
+
|
230 |
+
# # Gradio ChatInterface
|
231 |
+
# demo = gr.ChatInterface(
|
232 |
+
# fn=chat_fn,
|
233 |
+
# title="🩺 Apollo-7B Medical Chatbot",
|
234 |
+
# description="Multilingual (Arabic & English) medical Q&A chatbot powered by Apollo-7B. No RAG, just fast model inference.",
|
235 |
+
# theme=gr.themes.Soft()
|
236 |
+
# )
|
237 |
+
|
238 |
+
# if __name__ == "__main__":
|
239 |
+
# demo.launch(share=True)
|