File size: 5,803 Bytes
155a155
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
370ea7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
155a155
370ea7f
 
 
fd03aaa
370ea7f
 
 
 
155a155
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a214c06
2df899b
155a155
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f678de
155a155
 
 
3f678de
155a155
 
3f678de
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import streamlit as st
from openai import OpenAI
import time
import os
import logging
from groq import Groq
import os
import streamlit as st
import logging
from groq import Groq

# Logging setup
logging.basicConfig(level=logging.INFO)

# Streamlit page configuration
st.set_page_config(
    page_title="Gazal.ai-o1-preview",
    page_icon="🦌",
    layout="centered"
)

# Improved CSS for better contrast and design
st.markdown("""
<style>
    /* Background and main content styling */
    .stApp {
        background-color: #121212;
        color: #F5F5F5;
    }
    h1, h2, h3, h4, h5, h6 {
        color: #F5F5F5;
    }

    /* Chat bubbles styling */
    .user-message {
        text-align: right;
        background-color: #007AFF;
        color: white;
        padding: 0.5rem 1rem;
        border-radius: 15px;
        margin: 0.5rem 0;
        display: inline-block;
        max-width: 70%;
        box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.2);
    }
    .bot-message {
        text-align: left;
        background-color: #333333;
        color: #F5F5F5;
        padding: 0.5rem 1rem;
        border-radius: 15px;
        margin: 0.5rem 0;
        display: inline-block;
        max-width: 70%;
        box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.2);
    }

    /* Input field styling */
    input {
        background-color: #1E1E1E;
        color: #F5F5F5;
        border: 1px solid #333333;
        padding: 0.5rem;
        border-radius: 5px;
    }

    /* Button styling */
    button {
        background-color: #007AFF;
        color: white;
        border: none;
        border-radius: 5px;
        padding: 0.5rem 1rem;
        font-size: 1rem;
        cursor: pointer;
        box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.2);
    }
    button:hover {
        background-color: #005BB5;
    }

    /* Footer styling */
    .footer {
        text-align: center;
        margin-top: 2rem;
        color: #888888;
    }

    /* Disclaimer styling */
    .disclaimer {
        background-color: #333333;
        color: #F5F5F5;
        padding: 1rem;
        border-radius: 10px;
        margin-bottom: 20px;
        box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.2);
        font-size: 0.9rem;
    }
    .disclaimer h4 {
        color: #FF5733;
        margin: 0;
    }
</style>
<div class="disclaimer">
    <h4>Disclaimer</h4>
    <p>
        This app is for demonstration purposes only.
        The purpose of this demo is to showcase the power of reasoning large language models (LLMs) in guiding clinical decision support systems.
        It is not intended for clinical use. Please consult medical professionals for accurate medical advice.
    </p>
</div>
""", unsafe_allow_html=True)

# Groq API client initialization
@st.cache_resource
def init_groq_client():
    return Groq(api_key=os.getenv("GROQ_API_KEY"))

# Chat with Groq model
def chat_with_groq(client, message, history):
    try:
        # Build the conversation context
        messages = [
            {"role": "system", "content": "You are a helpful medical and clinical decision support system. Think step by step before answering."},
            *[{"role": "user" if i % 2 == 0 else "assistant", "content": m} for h in history for i, m in enumerate(h)],
            {"role": "user", "content": message}
        ]

        # Call the Groq model
        completion = client.chat.completions.create(
            model="deepseek-r1-distill-llama-70b",
            messages=messages,
            temperature=0.6,
            max_tokens=4000,
            top_p=0.95,
            stream=True,  # Stream the response
        )

        # Stream the response chunk by chunk
        response = ""
        for chunk in completion:
            content = chunk.choices[0].delta.content or ""
            response += content
            yield response

    except Exception as e:
        logging.error(f"Error during Groq inference: {str(e)}")
        yield f"An error occurred: {str(e)}. Please check your API key and network connection."

# Initialize app state
if "history" not in st.session_state:
    st.session_state["history"] = []  # [(user_message, bot_response), ...]

# Display the app title and description
st.title("Gazal.ai-o1-preview 🦌")
st.write("Ask gazal.ai any healthcare question and it will provide step-by-step reasoning.")

# Input form
with st.form("chat_form", clear_on_submit=True):
    user_message = st.text_input("Your Message:", key="user_input")
    submitted = st.form_submit_button("Send")

# Process user input and display chat
if submitted and user_message:
    # Add user message to history
    st.session_state["history"].append((user_message, None))

    # Display chat history
    for user_text, bot_text in st.session_state["history"]:
        st.markdown(f'<div class="user-message">{user_text}</div>', unsafe_allow_html=True)
        if bot_text:
            st.markdown(f'<div class="bot-message">{bot_text}</div>', unsafe_allow_html=True)

    # Initialize Groq client
    groq_client = init_groq_client()

    # Generate bot response
    response_placeholder = st.empty()  # Placeholder for streaming updates
    bot_response = ""
    for partial_response in chat_with_groq(groq_client, user_message, st.session_state["history"][:-1]):
        bot_response = partial_response  # Update bot response incrementally
        response_placeholder.markdown(f'<div class="bot-message">{bot_response}</div>', unsafe_allow_html=True)

    # Update history with full bot response
    st.session_state["history"][-1] = (user_message, bot_response)

# Clear chat history button
# Clear chat history button
if st.button("Clear Chat"):
    st.session_state["history"] = []
    st.rerun()

# Footer
st.markdown('<div class="footer">Gazal.ai © 2025 Made with ❤️ by TachyHealth</div>', unsafe_allow_html=True)