File size: 3,931 Bytes
f08ecfd
 
 
 
 
 
f11ecb6
f08ecfd
 
f9eea17
 
 
 
6b75565
 
f08ecfd
 
 
 
 
 
 
 
 
87dafb9
 
f08ecfd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3a2b2b0
f08ecfd
 
 
 
 
 
 
 
 
 
 
 
 
e9f54bc
 
 
 
50ded28
e9f54bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f08ecfd
 
e9f54bc
 
 
 
 
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import streamlit as st
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
from transformers import GPT2Tokenizer, GPT2LMHeadModel
from sentence_transformers import SentenceTransformer, util
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch

import gdown
import os
import pandas as pd

excel_file_path = 'medical_data.csv'

try:
    medical_df = pd.read_csv(excel_file_path, encoding='utf-8')
except UnicodeDecodeError:
    medical_df = pd.read_csv(excel_file_path, encoding='latin1')

# TF-IDF Vectorization
vectorizer = TfidfVectorizer(stop_words='english')
X_tfidf = vectorizer.fit_transform(medical_df['Questions'])

tokenizer = AutoTokenizer.from_pretrained("Josephgflowers/TinyLlama-3T-Cinder-v1.3")
model = AutoModelForCausalLM.from_pretrained("Josephgflowers/TinyLlama-3T-Cinder-v1.3")

# Load pre-trained Sentence Transformer model
sbert_model_name = "paraphrase-MiniLM-L6-v2"
sbert_model = SentenceTransformer(sbert_model_name)

# Function to answer medical questions using a combination of TF-IDF, LLM, and semantic similarity
def get_medical_response(question, vectorizer, X_tfidf, model, tokenizer, sbert_model, medical_df):
    # TF-IDF Cosine Similarity
    question_vector = vectorizer.transform([question])
    tfidf_similarities = cosine_similarity(question_vector, X_tfidf).flatten()

    # Find the most similar question using semantic similarity
    question_embedding = sbert_model.encode(question, convert_to_tensor=True)
    similarities = util.pytorch_cos_sim(question_embedding, sbert_model.encode(medical_df['Questions'].tolist(), convert_to_tensor=True)).flatten()
    max_sim_index = similarities.argmax().item()

    # LLM response generation
    input_text = "DiBot: " + medical_df.iloc[max_sim_index]['Questions']
    input_ids = tokenizer.encode(input_text, return_tensors="pt")
    attention_mask = torch.ones(input_ids.shape, dtype=torch.long)
    pad_token_id = tokenizer.eos_token_id
    lm_output = model.generate(input_ids, max_length=150, num_return_sequences=1, no_repeat_ngram_size=2, attention_mask=attention_mask, pad_token_id=pad_token_id)
    lm_generated_response = tokenizer.decode(lm_output[0], skip_special_tokens=True)

    # Compare similarities and choose the best response
    if tfidf_similarities.max() > 0.5:
        tfidf_index = tfidf_similarities.argmax()
        return medical_df.iloc[tfidf_index]['Answers']
    else:
        return lm_generated_response

# Custom CSS to enhance the UI
st.markdown("""
    <style>
        .main {
            background-color: ##131313;
            padding: 2rem;
            border-radius: 10px;
        }
        .stTextInput > div > div > input {
            border: 2px solid #ccc;
            border-radius: 10px;
            padding: 10px;
            width: 100%;
        }
        .stButton > button {
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 10px;
            padding: 10px 20px;
            cursor: pointer;
        }
        .stButton > button:hover {
            background-color: #45a049;
        }
        .stTextArea > div > div > textarea {
            border: 2px solid #ccc;
            border-radius: 10px;
            padding: 10px;
            width: 100%;
        }
    </style>
""", unsafe_allow_html=True)

# Streamlit app layout
st.title("🤖 DiBot - Your Medical Query Assistant")
st.write("Ask me any medical question, and I'll do my best to provide an accurate response.")

st.subheader("Enter your question below:")

user_input = st.text_input("Your question:", "")
if user_input.lower() == "exit":
    st.stop()

if user_input:
    response = get_medical_response(user_input, vectorizer, X_tfidf, model, tokenizer, sbert_model, medical_df)
    st.subheader("Bot's Response:")
    st.text_area("", response, height=200)