Spaces:
Running
Running
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,154 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Configure page settings (MUST BE FIRST STREAMLIT COMMAND)
|
2 |
+
import streamlit as st
|
3 |
+
from streamlit_option_menu import option_menu
|
4 |
+
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM
|
5 |
+
from PyPDF2 import PdfReader
|
6 |
+
|
7 |
+
# Set page config
|
8 |
+
st.set_page_config(
|
9 |
+
page_title="Disease Analysis GPT",
|
10 |
+
layout="wide",
|
11 |
+
initial_sidebar_state="expanded"
|
12 |
+
)
|
13 |
+
|
14 |
+
# Load Hugging Face models and tokenizer for text generation
|
15 |
+
@st.cache_resource
|
16 |
+
def load_model():
|
17 |
+
tokenizer = AutoTokenizer.from_pretrained("harishussain12/Disease_Managment")
|
18 |
+
model = AutoModelForCausalLM.from_pretrained("harishussain12/Disease_Managment")
|
19 |
+
return tokenizer, model
|
20 |
+
|
21 |
+
# Function to create a text generation pipeline
|
22 |
+
@st.cache_resource
|
23 |
+
def create_pipeline():
|
24 |
+
tokenizer, model = load_model()
|
25 |
+
return pipeline("text-generation", model=model, tokenizer=tokenizer)
|
26 |
+
|
27 |
+
# Function to extract text from PDF file
|
28 |
+
def read_pdf(file):
|
29 |
+
try:
|
30 |
+
reader = PdfReader(file)
|
31 |
+
text = ""
|
32 |
+
for page in reader.pages:
|
33 |
+
text += page.extract_text()
|
34 |
+
return text
|
35 |
+
except Exception as e:
|
36 |
+
return f"Error reading PDF: {e}"
|
37 |
+
|
38 |
+
# Load pipelines
|
39 |
+
text_pipeline = create_pipeline()
|
40 |
+
|
41 |
+
# Custom CSS for styling
|
42 |
+
st.markdown(
|
43 |
+
"""
|
44 |
+
<style>
|
45 |
+
body {
|
46 |
+
font-family: 'Arial', sans-serif;
|
47 |
+
}
|
48 |
+
.stButton button {
|
49 |
+
background-color: #0b2545;
|
50 |
+
color: white;
|
51 |
+
border: none;
|
52 |
+
border-radius: 25px;
|
53 |
+
padding: 8px 20px;
|
54 |
+
font-size: 14px;
|
55 |
+
font-weight: bold;
|
56 |
+
cursor: pointer;
|
57 |
+
}
|
58 |
+
.stButton button:hover {
|
59 |
+
background-color: #0a1b35;
|
60 |
+
}
|
61 |
+
.search-box {
|
62 |
+
border-radius: 20px;
|
63 |
+
border: 1px solid #ccc;
|
64 |
+
padding: 10px;
|
65 |
+
width: 100%;
|
66 |
+
font-size: 16px;
|
67 |
+
background-color: #ffffff;
|
68 |
+
}
|
69 |
+
.info-box {
|
70 |
+
background-color: #f8f9fa;
|
71 |
+
border-left: 5px solid #0b2545;
|
72 |
+
padding: 15px;
|
73 |
+
border-radius: 5px;
|
74 |
+
font-size: 14px;
|
75 |
+
}
|
76 |
+
</style>
|
77 |
+
""",
|
78 |
+
unsafe_allow_html=True
|
79 |
+
)
|
80 |
+
|
81 |
+
# Sidebar
|
82 |
+
with st.sidebar:
|
83 |
+
new_chat_button = st.button("New Chat", key="new_chat", help="Start a new chat to ask a different question.")
|
84 |
+
if new_chat_button:
|
85 |
+
st.session_state.clear() # Clear session state to simulate a new chat
|
86 |
+
|
87 |
+
selected = option_menu(
|
88 |
+
menu_title=None,
|
89 |
+
options=[" Home", " Discover"],
|
90 |
+
icons=["house", "search"],
|
91 |
+
menu_icon="cast",
|
92 |
+
default_index=0,
|
93 |
+
styles={
|
94 |
+
"container": {"padding": "0!important", "background-color": "#3e4a5b"},
|
95 |
+
"icon": {"color": "#ffffff", "font-size": "16px"},
|
96 |
+
"nav-link": {
|
97 |
+
"font-size": "15px",
|
98 |
+
"text-align": "left",
|
99 |
+
"margin": "0px",
|
100 |
+
"color": "#ffffff",
|
101 |
+
"font-weight": "bold",
|
102 |
+
"padding": "10px 20px",
|
103 |
+
},
|
104 |
+
"nav-link-selected": {"background-color": "#0b2545", "color": "white"},
|
105 |
+
}
|
106 |
+
)
|
107 |
+
|
108 |
+
# Main content
|
109 |
+
col1, col2, col3 = st.columns([1, 2, 1])
|
110 |
+
|
111 |
+
with col2:
|
112 |
+
st.markdown("<h1 style='text-align: center;'>Disease Analysis GPT</h1>", unsafe_allow_html=True)
|
113 |
+
st.markdown("<h3 style='text-align: center;'>What do you want to know?</h3>", unsafe_allow_html=True)
|
114 |
+
|
115 |
+
# Model selection (now including Document Analysis)
|
116 |
+
model_selection = st.selectbox(
|
117 |
+
"Select a model",
|
118 |
+
options=["Disease Analysis", "Document Analysis"],
|
119 |
+
index=0
|
120 |
+
)
|
121 |
+
|
122 |
+
# If the user selects Document Analysis, show an error and prompt them to switch to Disease Analysis
|
123 |
+
if model_selection == "Document Analysis":
|
124 |
+
st.error("Please switch to 'Disease Analysis' model for generating responses. Document Analysis is not available in this version.")
|
125 |
+
|
126 |
+
# Search box
|
127 |
+
search_input = st.text_input(
|
128 |
+
"",
|
129 |
+
placeholder="Type your question here...",
|
130 |
+
label_visibility="collapsed",
|
131 |
+
help="Ask anything related to disease management."
|
132 |
+
)
|
133 |
+
|
134 |
+
# File upload below search box
|
135 |
+
uploaded_file = st.file_uploader("Upload a PDF file", type="pdf", help="Attach relevant files or documents to your query.")
|
136 |
+
|
137 |
+
if search_input:
|
138 |
+
with st.spinner("Generating response..."):
|
139 |
+
try:
|
140 |
+
if model_selection == "Disease Analysis":
|
141 |
+
context = ""
|
142 |
+
if uploaded_file is not None:
|
143 |
+
file_content = read_pdf(uploaded_file)
|
144 |
+
if "Error" in file_content:
|
145 |
+
st.error(file_content)
|
146 |
+
else:
|
147 |
+
context = file_content
|
148 |
+
|
149 |
+
query_input = search_input + (f"\n\nContext:\n{context}" if context else "")
|
150 |
+
response = text_pipeline(query_input, max_length=200, num_return_sequences=1)
|
151 |
+
st.markdown(f"### Response:\n{response[0]['generated_text']}")
|
152 |
+
|
153 |
+
except Exception as e:
|
154 |
+
st.error(f"Error generating response: {str(e)}")
|