CoolFace
Apppublic

asadAbdullah/Gen_Disorder

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app.py104 linesDownload Raw Back to root
1import streamlit as st2from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM3from sentence_transformers import SentenceTransformer4import faiss5import numpy as np6import os7 8# Load Hugging Face models and tokenizers9os.environ['HF_HOME'] = '/path_to_huggingface_cache'  # Optional, set a cache directory10 11# Load a model for text generation (like GPT-2, GPT-3, or another language model)12llm_model = AutoModelForCausalLM.from_pretrained("gpt2")  # Use GPT-2 for this example13llm_tokenizer = AutoTokenizer.from_pretrained("gpt2")14 15# Load Sentence Transformers for embeddings (this example uses `all-MiniLM-L6-v2`)16embedding_model = SentenceTransformer('all-MiniLM-L6-v2')17 18# Create a FAISS index for storing the embeddings19def create_faiss_index(texts):20    embeddings = embedding_model.encode(texts)21    dim = embeddings.shape[1]22    index = faiss.IndexFlatL2(dim)23    index.add(np.array(embeddings).astype(np.float32))24    return index, embeddings25 26def get_faiss_search(index, query, top_k=1):27    query_embedding = embedding_model.encode([query])28    _, I = index.search(np.array(query_embedding).astype(np.float32), top_k)29    return I30 31# Handle conversation history manually32def update_conversation_history(user_question, bot_answer, history):33    history.append({"role": "user", "content": user_question})34    history.append({"role": "assistant", "content": bot_answer})35    return history36 37# Generate response using GPT-2 (or any other LLM)38def generate_response(question, context):39    input_text = f"Context: {context} Question: {question}"40    inputs = llm_tokenizer(input_text, return_tensors="pt", truncation=True, padding=True)41    outputs = llm_model.generate(**inputs)42    answer = llm_tokenizer.decode(outputs[0], skip_special_tokens=True)43    return answer44 45# Define the Streamlit app logic46def user_input(user_question, vector_store, conversation_history):47    # Search for relevant context in the vector store using FAISS48    search_results = get_faiss_search(vector_store, user_question, top_k=1)49    # Assuming the FAISS index stores documents, get the most relevant one50    context = " ".join([documents[i] for i in search_results[0]])51 52    # Generate a response using the context and the user question53    bot_answer = generate_response(user_question, context)54 55    # Update the conversation history56    conversation_history = update_conversation_history(user_question, bot_answer, conversation_history)57 58    # Display the conversation history59    for message in conversation_history:60        if message['role'] == 'user':61            st.write("Human: ", message['content'])62        else:63            st.write("Bot: ", message['content'])64 65    return conversation_history66 67# Streamlit app setup68def main():69    st.set_page_config("Medical-Query-site")70    st.header("Post your query")71 72    # Initialize conversation history73    if 'conversation_history' not in st.session_state:74        st.session_state['conversation_history'] = []75 76    # Display the user input form77    user_question = st.text_input("Feel free to ask anything")78 79    if user_question:80        # Initialize or load the FAISS index if it's not already loaded81        if 'faiss_index' not in st.session_state:82            # Example documents (this should be your actual document collection)83            global documents84            documents = [85                "Symptoms of COVID-19 include fever, cough, and shortness of breath.",86                "To lower blood pressure, one should exercise regularly, reduce salt intake, and manage stress.",87                "Diabetes management includes maintaining a healthy diet, regular exercise, and medication.",88                "Common cold can be treated with rest, hydration, and over-the-counter medications."89            ]90            st.session_state.faiss_index, _ = create_faiss_index(documents)91 92        # Retrieve the answer based on user input93        st.session_state.conversation_history = user_input(user_question, st.session_state.faiss_index, st.session_state.conversation_history)94 95    # Display the sidebar settings96    with st.sidebar:97        st.title("Settings")98        st.subheader("Please initiate the app")99        if st.button("Wake the bot"):100            st.success("Bot is awake and ready to answer!")101 102if __name__ == "__main__":103    main()104