Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,88 +1,89 @@
|
|
| 1 |
-
# Author: Bastien & Pascal
|
| 2 |
-
# Date: 2/25/2024
|
| 3 |
-
# Project: SAE-GPT2 | BUT 3 Informatique - Semester 5
|
| 4 |
-
|
| 5 |
-
# Import of required libraries
|
| 6 |
-
import os
|
| 7 |
-
|
| 8 |
-
os.system("pip install --upgrade pip")
|
| 9 |
-
os.system("pip install googletrans-py")
|
| 10 |
-
os.system("pip install tensorflow==2.15.0")
|
| 11 |
-
os.system("pip install keras-nlp")
|
| 12 |
-
os.system("pip install -q --upgrade keras") # Upgrade Keras to version 3
|
| 13 |
-
|
| 14 |
-
import time
|
| 15 |
-
import keras
|
| 16 |
-
import keras_nlp
|
| 17 |
-
import pandas as pd
|
| 18 |
-
import gradio as gr
|
| 19 |
-
from googletrans import Translator
|
| 20 |
-
from importHuggingFaceHubModel import from_pretrained_keras
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
#
|
| 27 |
-
model =
|
| 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 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
|
|
|
|
|
| 1 |
+
# Author: Bastien & Pascal
|
| 2 |
+
# Date: 2/25/2024
|
| 3 |
+
# Project: SAE-GPT2 | BUT 3 Informatique - Semester 5
|
| 4 |
+
|
| 5 |
+
# Import of required libraries
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
os.system("pip install --upgrade pip")
|
| 9 |
+
os.system("pip install googletrans-py")
|
| 10 |
+
os.system("pip install tensorflow==2.15.0")
|
| 11 |
+
os.system("pip install keras-nlp")
|
| 12 |
+
os.system("pip install -q --upgrade keras") # Upgrade Keras to version 3
|
| 13 |
+
|
| 14 |
+
import time
|
| 15 |
+
import keras
|
| 16 |
+
import keras_nlp
|
| 17 |
+
import pandas as pd
|
| 18 |
+
import gradio as gr
|
| 19 |
+
from googletrans import Translator
|
| 20 |
+
#from importHuggingFaceHubModel import from_pretrained_keras
|
| 21 |
+
from huggingface_hub import from_pretrained_keras
|
| 22 |
+
|
| 23 |
+
# Set Keras Backend to Tensorflow
|
| 24 |
+
os.environ["KERAS_BACKEND"] = "tensorflow"
|
| 25 |
+
|
| 26 |
+
# Load the fine-tuned model
|
| 27 |
+
#model = keras.models.load_model("LoRA_Model_V2.keras")
|
| 28 |
+
model = from_pretrained_keras('DracolIA/GPT-2-LoRA-HealthCare')
|
| 29 |
+
|
| 30 |
+
translator = Translator() # Create Translator Instance
|
| 31 |
+
|
| 32 |
+
# Function to generate responses from the model
|
| 33 |
+
def generate_responses(question):
|
| 34 |
+
language = translator.detect(question).lang.upper() # Verify the language of the prompt
|
| 35 |
+
if language != "EN":
|
| 36 |
+
question = translator.translate(question, src=language, dest="en").text # Translation of user text to english for the model
|
| 37 |
+
|
| 38 |
+
prompt = f"[QUESTION] {question} [ANSWER]"
|
| 39 |
+
# Generate the answer from the model and then clean and extract the real model's response from the prompt engineered string
|
| 40 |
+
output = clean_answer_text(model.generate(prompt, max_length=1024))
|
| 41 |
+
|
| 42 |
+
# Generate the answer from the model and then clean and extract the real model's response from the prompt engineered string
|
| 43 |
+
if language != "EN":
|
| 44 |
+
output = Translator().translate(output, src="en", dest=language).text # Translation of model's text to user's language
|
| 45 |
+
|
| 46 |
+
return output
|
| 47 |
+
|
| 48 |
+
# Function clean the output of the model from the prompt engineering done in the "generate_responses" function
|
| 49 |
+
def clean_answer_text(text: str) -> str:
|
| 50 |
+
# Define the start marker for the model's response
|
| 51 |
+
response_start = text.find("[ANSWER]") + len("[ANSWER]")
|
| 52 |
+
|
| 53 |
+
# Extract everything after "Doctor:"
|
| 54 |
+
response_text = text[response_start:].strip()
|
| 55 |
+
last_dot_index = response_text.rfind(".")
|
| 56 |
+
if last_dot_index != -1:
|
| 57 |
+
response_text = response_text[:last_dot_index + 1]
|
| 58 |
+
|
| 59 |
+
# Additional cleaning if necessary (e.g., removing leading/trailing spaces or new lines)
|
| 60 |
+
response_text = response_text.strip()
|
| 61 |
+
|
| 62 |
+
return response_text
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
# Define a Gradio interface
|
| 66 |
+
def chat_interface(question, history_df):
|
| 67 |
+
response = generate_responses(question)
|
| 68 |
+
# Insert the new question and response at the beginning of the DataFrame
|
| 69 |
+
history_df = pd.concat([pd.DataFrame({"Question": [question], "Réponse": [response]}), history_df], ignore_index=True)
|
| 70 |
+
return response, history_df
|
| 71 |
+
|
| 72 |
+
with gr.Blocks() as demo:
|
| 73 |
+
gr.HTML("""
|
| 74 |
+
<div style='width: 100%; height: 200px; background: url("https://github.com/BastienHot/SAE-GPT2/raw/70fb88500a2cc168d71e8ed635fc54492beb6241/image/logo.png") no-repeat center center; background-size: contain;'>
|
| 75 |
+
<h1 style='text-align:center; width=100%'>DracolIA - AI Question Answering for Healthcare</h1>
|
| 76 |
+
</div>
|
| 77 |
+
""")
|
| 78 |
+
with gr.Row():
|
| 79 |
+
question = gr.Textbox(label="Votre Question", placeholder="Saisissez ici...")
|
| 80 |
+
submit_btn = gr.Button("Envoyer")
|
| 81 |
+
response = gr.Textbox(label="Réponse", interactive=False)
|
| 82 |
+
|
| 83 |
+
# Initialize an empty DataFrame to keep track of question-answer history
|
| 84 |
+
history_display = gr.Dataframe(headers=["Question", "Réponse"], values=[], interactive=False)
|
| 85 |
+
|
| 86 |
+
submit_btn.click(fn=chat_interface, inputs=[question, history_display], outputs=[response, history_display])
|
| 87 |
+
|
| 88 |
+
if __name__ == "__main__":
|
| 89 |
+
demo.launch()
|