File size: 3,233 Bytes
efd757b
aacdac0
 
 
efd757b
aacdac0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
import os

# Apply custom CSS for retro 80s green theme
def apply_custom_css():
    try:
        with open("style.css") as f:
            st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
    except FileNotFoundError:
        st.warning("style.css not found. Using default styles.")

@st.cache_resource
def load_model():
    model_path = "HuggingFaceH4/zephyr-7b-beta"
    peft_model_path = "yitzashapiro/FDA-guidance-zephyr-7b-beta-PEFT"
    
    try:
        tokenizer = AutoTokenizer.from_pretrained(model_path)
        model = AutoModelForCausalLM.from_pretrained(
            model_path,
            device_map="auto",
            torch_dtype=torch.float16  # Adjust if necessary
        ).eval()
        model.load_adapter(peft_model_path)
        st.success("Model loaded successfully.")
    except Exception as e:
        st.error(f"Error loading model: {e}")
        st.stop()
    
    return tokenizer, model

def generate_response(tokenizer, model, user_input):
    messages = [
        {"role": "user", "content": user_input}
    ]
    
    try:
        if hasattr(tokenizer, 'apply_chat_template'):
            input_ids = tokenizer.apply_chat_template(
                conversation=messages, 
                max_length=45, 
                tokenize=True, 
                add_generation_prompt=True, 
                return_tensors='pt'
            )
        else:
            input_ids = tokenizer(
                user_input, 
                return_tensors='pt', 
                truncation=True, 
                max_length=45
            )['input_ids']
        
        pad_token_id = tokenizer.pad_token_id if tokenizer.pad_token_id is not None else 0
        attention_mask = (input_ids != pad_token_id).long()
        
        output_ids = model.generate(
            input_ids.to(model.device),
            max_length=2048,
            max_new_tokens=500,
            attention_mask=attention_mask.to(model.device)
        )
        
        response = tokenizer.decode(output_ids[0][input_ids.shape[1]:], skip_special_tokens=True)
        return response
    except Exception as e:
        st.error(f"Error generating response: {e}")
        return "An error occurred while generating the response."

def main():
    apply_custom_css()
    
    st.set_page_config(page_title="FDA NDA Submission Assistant", layout="centered")
    st.title("FDA NDA Submission Assistant")
    st.write("Ask the model about submitting an NDA to the FDA.")
    
    tokenizer, model = load_model()
    
    user_input = st.text_input("Enter your question:", "What's the best way to submit an NDA to the FDA?")
    
    if st.button("Generate Response"):
        if user_input.strip() == "":
            st.error("Please enter a valid question.")
        else:
            try:
                with st.spinner("Generating response..."):
                    response = generate_response(tokenizer, model, user_input)
                st.success("Response:")
                st.write(response)
            except Exception as e:
                st.error(f"An error occurred: {e}")

if __name__ == "__main__":
    main()