Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -18,6 +18,14 @@ import io
|
|
18 |
import hashlib
|
19 |
import datetime
|
20 |
from utils import build_logger
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
dotenv.load_dotenv()
|
23 |
|
@@ -312,33 +320,76 @@ def convert_to_markdown(vectara_response_json):
|
|
312 |
return f"{markdown_summary}**Sources:**\n{markdown_sources}"
|
313 |
else:
|
314 |
return "No data found in the response."
|
315 |
-
# Main function to handle the Gradio interface logic
|
316 |
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
-
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
342 |
|
343 |
def process_and_query(input_language=None, audio_input=None, image_input=None, text_input=None):
|
344 |
try:
|
@@ -378,7 +429,7 @@ def process_and_query(input_language=None, audio_input=None, image_input=None, t
|
|
378 |
markdown_output += "\n\n**Original Image Description:**\n" + image_description
|
379 |
|
380 |
# Process the summary with OpenAI
|
381 |
-
final_response =
|
382 |
print("Final Response:", final_response) # Debug print
|
383 |
|
384 |
# Evaluate hallucination
|
@@ -396,15 +447,17 @@ def process_and_query(input_language=None, audio_input=None, image_input=None, t
|
|
396 |
welcome_message = """
|
397 |
# 👋🏻Welcome to ⚕🗣️😷MultiMed - Access Chat ⚕🗣️😷
|
398 |
|
399 |
-
🗣️📝 This is an educational and accessible conversational tool
|
400 |
|
401 |
### How To Use ⚕🗣️😷MultiMed⚕:
|
402 |
|
403 |
🗣️📝Interact with ⚕🗣️😷MultiMed⚕ in any language using image, audio or text!
|
404 |
|
405 |
-
📚🌟💼
|
|
|
|
|
406 |
|
407 |
-
|
408 |
"""
|
409 |
|
410 |
|
@@ -520,14 +573,10 @@ with gr.Blocks(theme='ParityError/Anime') as iface :
|
|
520 |
input_language = gr.Dropdown(languages, label="select the language",value="English",interactive=True)
|
521 |
audio_input = gr.Audio(label="speak",type="filepath",sources="microphone")
|
522 |
audio_output = gr.Markdown(label="output text")
|
523 |
-
# audio_button = gr.Button("process audio")
|
524 |
-
# audio_button.click(process_speech, inputs=[input_language,audio_input], outputs=audio_output)
|
525 |
gr.Examples([["English","sample_input.mp3"]],inputs=[input_language,audio_input])
|
526 |
with gr.Accordion("image identification",open=True):
|
527 |
image_input = gr.Image(label="upload image")
|
528 |
image_output = gr.Markdown(label="output text")
|
529 |
-
# image_button = gr.Button("process image")
|
530 |
-
# image_button.click(process_image, inputs=image_input, outputs=image_output)
|
531 |
gr.Examples(["sick person.jpeg"],inputs=[image_input])
|
532 |
with gr.Accordion("text summarization",open=True):
|
533 |
text_input = gr.Textbox(label="input text",lines=5)
|
@@ -545,15 +594,5 @@ with gr.Blocks(theme='ParityError/Anime') as iface :
|
|
545 |
["구강 헤르페스의 적절한 치료법은 무엇입니까?"],
|
546 |
["Je, ni matibabu gani sahihi kwa herpes ya buccal?"],
|
547 |
],inputs=[text_input])
|
548 |
-
# with gr.Accordion("hallucination check",open=True):
|
549 |
-
# assertion = gr.Textbox(label="assertion")
|
550 |
-
# citation = gr.Textbox(label="citation text")
|
551 |
-
# hullucination_output = gr.Markdown(label="output text")
|
552 |
-
# hallucination_button = gr.Button("check hallucination")
|
553 |
-
# gr.Examples([["i am drunk","sarah is pregnant"]],inputs=[assertion,citation])
|
554 |
-
# hallucination_button.click(check_hallucination,inputs=[assertion,citation],outputs=hullucination_output)
|
555 |
-
|
556 |
-
|
557 |
-
|
558 |
|
559 |
iface.queue().launch(show_error=True,debug=True)
|
|
|
18 |
import hashlib
|
19 |
import datetime
|
20 |
from utils import build_logger
|
21 |
+
from transformers import AutoTokenizer, MistralForCausalLM
|
22 |
+
import torch
|
23 |
+
import random
|
24 |
+
from textwrap import wrap
|
25 |
+
from transformers import AutoConfig, AutoTokenizer, AutoModelForSeq2SeqLM, AutoModelForCausalLM, MistralForCausalLM
|
26 |
+
from peft import PeftModel, PeftConfig
|
27 |
+
import torch
|
28 |
+
import os
|
29 |
|
30 |
dotenv.load_dotenv()
|
31 |
|
|
|
320 |
return f"{markdown_summary}**Sources:**\n{markdown_sources}"
|
321 |
else:
|
322 |
return "No data found in the response."
|
|
|
323 |
|
324 |
+
# Functions to Wrap the Prompt Correctly
|
325 |
+
def wrap_text(text, width=90):
|
326 |
+
lines = text.split('\n')
|
327 |
+
wrapped_lines = [textwrap.fill(line, width=width) for line in lines]
|
328 |
+
wrapped_text = '\n'.join(wrapped_lines)
|
329 |
+
return wrapped_text
|
330 |
+
def multimodal_prompt(user_input, system_prompt="You are an expert medical analyst:"):
|
331 |
+
|
332 |
+
# Combine user input and system prompt
|
333 |
+
formatted_input = f"{user_input}{system_prompt}"
|
334 |
+
|
335 |
+
# Encode the input text
|
336 |
+
encodeds = tokenizer(formatted_input, return_tensors="pt", add_special_tokens=False)
|
337 |
+
model_inputs = encodeds.to(device)
|
338 |
+
|
339 |
+
# Generate a response using the model
|
340 |
+
output = model.generate(
|
341 |
+
**model_inputs,
|
342 |
+
max_length=max_length,
|
343 |
+
use_cache=True,
|
344 |
+
early_stopping=True,
|
345 |
+
bos_token_id=model.config.bos_token_id,
|
346 |
+
eos_token_id=model.config.eos_token_id,
|
347 |
+
pad_token_id=model.config.eos_token_id,
|
348 |
+
temperature=0.1,
|
349 |
+
do_sample=True
|
350 |
+
)
|
351 |
+
|
352 |
+
# Decode the response
|
353 |
+
response_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
354 |
+
|
355 |
+
return response_text
|
356 |
+
|
357 |
+
# Define the device
|
358 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
359 |
+
|
360 |
+
# Use the base model's ID
|
361 |
+
base_model_id = "stabilityai/stablelm-3b-4e1t"
|
362 |
+
model_directory = "Tonic/stablemed"
|
363 |
+
|
364 |
+
# Instantiate the Tokenizer
|
365 |
+
tokenizer = AutoTokenizer.from_pretrained("stabilityai/stablelm-3b-4e1t", token=hf_token, trust_remote_code=True, padding_side="left")
|
366 |
+
# tokenizer = AutoTokenizer.from_pretrained("Tonic/stablemed", trust_remote_code=True, padding_side="left")
|
367 |
+
tokenizer.pad_token = tokenizer.eos_token
|
368 |
+
tokenizer.padding_side = 'left'
|
369 |
+
|
370 |
+
# Load the PEFT model
|
371 |
+
peft_config = PeftConfig.from_pretrained("Tonic/stablemed", token=hf_token)
|
372 |
+
peft_model = AutoModelForCausalLM.from_pretrained("stabilityai/stablelm-3b-4e1t", token=hf_token, trust_remote_code=True)
|
373 |
+
peft_model = PeftModel.from_pretrained(peft_model, "Tonic/stablemed", token=hf_token)
|
374 |
+
|
375 |
+
|
376 |
+
class ChatBot:
|
377 |
+
def __init__(self):
|
378 |
+
self.history = []
|
379 |
+
|
380 |
+
def predict(self, user_input, system_prompt="You are an expert medical analyst:"):
|
381 |
+
formatted_input = f"<s>[INST]{system_prompt} {user_input}[/INST]"
|
382 |
+
user_input_ids = tokenizer.encode(formatted_input, return_tensors="pt")
|
383 |
+
response = peft_model.generate(input_ids=user_input_ids, max_length=512, pad_token_id=tokenizer.eos_token_id)
|
384 |
+
response_text = tokenizer.decode(response[0], skip_special_tokens=True)
|
385 |
+
return response_text
|
386 |
+
|
387 |
+
def process_summary_with_stablemed(summary):
|
388 |
+
system_prompt = "You are a clinical education consultant ready to do just about anything for your students. You are discussing training cases with students at TonicUniversity. Assess and describe the proper options to your students in minute detail. Propose a course of action for them to base their recommendations on based on your description. Always provide a complete answer. Exclude any other commentary:"
|
389 |
+
response_text = bot.predict(summary, system_prompt)
|
390 |
+
return response_text
|
391 |
+
|
392 |
+
# Main function to handle the Gradio interface logic
|
393 |
|
394 |
def process_and_query(input_language=None, audio_input=None, image_input=None, text_input=None):
|
395 |
try:
|
|
|
429 |
markdown_output += "\n\n**Original Image Description:**\n" + image_description
|
430 |
|
431 |
# Process the summary with OpenAI
|
432 |
+
final_response = process_summary_with_stablemed(markdown_output)
|
433 |
print("Final Response:", final_response) # Debug print
|
434 |
|
435 |
# Evaluate hallucination
|
|
|
447 |
welcome_message = """
|
448 |
# 👋🏻Welcome to ⚕🗣️😷MultiMed - Access Chat ⚕🗣️😷
|
449 |
|
450 |
+
🗣️📝 This is an educational and accessible conversational tool.
|
451 |
|
452 |
### How To Use ⚕🗣️😷MultiMed⚕:
|
453 |
|
454 |
🗣️📝Interact with ⚕🗣️😷MultiMed⚕ in any language using image, audio or text!
|
455 |
|
456 |
+
📚🌟💼 that uses [Tonic/stablemed](https://huggingface.co/Tonic/stablemed) and [adept/fuyu-8B](https://huggingface.co/adept/fuyu-8b) with [Vectara](https://huggingface.co/vectara) embeddings + retrieval.
|
457 |
+
do [get in touch](https://discord.gg/GWpVpekp). You can also use 😷MultiMed⚕️ on your own data & in your own way by cloning this space. 🧬🔬🔍 Simply click here: <a style="display:inline-block" href="https://huggingface.co/spaces/TeamTonic/MultiMed?duplicate=true"><img src="https://img.shields.io/badge/-Duplicate%20Space-blue?labelColor=white&style=flat&logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAP5JREFUOE+lk7FqAkEURY+ltunEgFXS2sZGIbXfEPdLlnxJyDdYB62sbbUKpLbVNhyYFzbrrA74YJlh9r079973psed0cvUD4A+4HoCjsA85X0Dfn/RBLBgBDxnQPfAEJgBY+A9gALA4tcbamSzS4xq4FOQAJgCDwV2CPKV8tZAJcAjMMkUe1vX+U+SMhfAJEHasQIWmXNN3abzDwHUrgcRGmYcgKe0bxrblHEB4E/pndMazNpSZGcsZdBlYJcEL9Afo75molJyM2FxmPgmgPqlWNLGfwZGG6UiyEvLzHYDmoPkDDiNm9JR9uboiONcBXrpY1qmgs21x1QwyZcpvxt9NS09PlsPAAAAAElFTkSuQmCC&logoWidth=14" alt="Duplicate Space"></a></h3>
|
458 |
+
### Join us :
|
459 |
|
460 |
+
🌟TeamTonic🌟 is always making cool demos! Join our active builder's🛠️community on 👻Discord: [Discord](https://discord.gg/GWpVpekp) On 🤗Huggingface: [TeamTonic](https://huggingface.co/TeamTonic) & [MultiTransformer](https://huggingface.co/MultiTransformer) On 🌐Github: [Polytonic](https://github.com/tonic-ai) & contribute to 🌟 [PolyGPT](https://github.com/tonic-ai/polygpt-alpha)"
|
461 |
"""
|
462 |
|
463 |
|
|
|
573 |
input_language = gr.Dropdown(languages, label="select the language",value="English",interactive=True)
|
574 |
audio_input = gr.Audio(label="speak",type="filepath",sources="microphone")
|
575 |
audio_output = gr.Markdown(label="output text")
|
|
|
|
|
576 |
gr.Examples([["English","sample_input.mp3"]],inputs=[input_language,audio_input])
|
577 |
with gr.Accordion("image identification",open=True):
|
578 |
image_input = gr.Image(label="upload image")
|
579 |
image_output = gr.Markdown(label="output text")
|
|
|
|
|
580 |
gr.Examples(["sick person.jpeg"],inputs=[image_input])
|
581 |
with gr.Accordion("text summarization",open=True):
|
582 |
text_input = gr.Textbox(label="input text",lines=5)
|
|
|
594 |
["구강 헤르페스의 적절한 치료법은 무엇입니까?"],
|
595 |
["Je, ni matibabu gani sahihi kwa herpes ya buccal?"],
|
596 |
],inputs=[text_input])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
597 |
|
598 |
iface.queue().launch(show_error=True,debug=True)
|