Spaces:
Running
Running
Upload 2 files
Browse files- app.py +133 -0
- requirements.txt +4 -0
app.py
ADDED
@@ -0,0 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from openai import OpenAI
|
3 |
+
import time
|
4 |
+
import os
|
5 |
+
import logging
|
6 |
+
from groq import Groq
|
7 |
+
|
8 |
+
# Logging setup
|
9 |
+
logging.basicConfig(level=logging.INFO)
|
10 |
+
|
11 |
+
# Streamlit page configuration
|
12 |
+
st.set_page_config(
|
13 |
+
page_title="Groq AI Reasoning Chatbot",
|
14 |
+
page_icon="🤖",
|
15 |
+
layout="centered"
|
16 |
+
)
|
17 |
+
|
18 |
+
# Custom CSS for better visuals
|
19 |
+
st.markdown("""
|
20 |
+
<style>
|
21 |
+
.stApp {
|
22 |
+
background-color: #F5F5F5;
|
23 |
+
}
|
24 |
+
.chat-container {
|
25 |
+
background-color: #FFFFFF;
|
26 |
+
margin: 1rem 0;
|
27 |
+
padding: 1rem;
|
28 |
+
border-radius: 10px;
|
29 |
+
box-shadow: 0px 4px 6px rgba(0, 0, 0, 0.1);
|
30 |
+
}
|
31 |
+
.user-message {
|
32 |
+
text-align: right;
|
33 |
+
background-color: #007AFF;
|
34 |
+
color: white;
|
35 |
+
padding: 0.5rem 1rem;
|
36 |
+
border-radius: 15px;
|
37 |
+
margin: 0.5rem 0;
|
38 |
+
display: inline-block;
|
39 |
+
max-width: 70%;
|
40 |
+
}
|
41 |
+
.bot-message {
|
42 |
+
text-align: left;
|
43 |
+
background-color: #E9ECEF;
|
44 |
+
padding: 0.5rem 1rem;
|
45 |
+
border-radius: 15px;
|
46 |
+
margin: 0.5rem 0;
|
47 |
+
display: inline-block;
|
48 |
+
max-width: 70%;
|
49 |
+
}
|
50 |
+
</style>
|
51 |
+
""", unsafe_allow_html=True)
|
52 |
+
|
53 |
+
# Groq API client initialization
|
54 |
+
@st.cache_resource
|
55 |
+
def init_groq_client():
|
56 |
+
return Groq(api_key=os.getenv("GROQ_API_KEY"))
|
57 |
+
|
58 |
+
# Chat with Groq model
|
59 |
+
def chat_with_groq(client, message, history):
|
60 |
+
try:
|
61 |
+
# Build the conversation context
|
62 |
+
messages = [
|
63 |
+
{"role": "system", "content": "You are a helpful assistant. Think step by step before answering."},
|
64 |
+
*[{"role": "user" if i % 2 == 0 else "assistant", "content": m} for h in history for i, m in enumerate(h)],
|
65 |
+
{"role": "user", "content": message}
|
66 |
+
]
|
67 |
+
|
68 |
+
# Call the Groq model
|
69 |
+
completion = client.chat.completions.create(
|
70 |
+
model="deepseek-r1-distill-llama-70b",
|
71 |
+
messages=messages,
|
72 |
+
temperature=0.6,
|
73 |
+
max_tokens=1024,
|
74 |
+
top_p=0.95,
|
75 |
+
stream=True, # Stream the response
|
76 |
+
)
|
77 |
+
|
78 |
+
# Stream the response chunk by chunk
|
79 |
+
response = ""
|
80 |
+
for chunk in completion:
|
81 |
+
content = chunk.choices[0].delta.content or ""
|
82 |
+
response += content
|
83 |
+
yield response
|
84 |
+
|
85 |
+
except Exception as e:
|
86 |
+
logging.error(f"Error during Groq inference: {str(e)}")
|
87 |
+
yield f"An error occurred: {str(e)}. Please check your API key and network connection."
|
88 |
+
|
89 |
+
# Initialize app state
|
90 |
+
if "history" not in st.session_state:
|
91 |
+
st.session_state["history"] = [] # [(user_message, bot_response), ...]
|
92 |
+
|
93 |
+
# Display the app title and description
|
94 |
+
st.title("Groq AI Reasoning Chatbot 🤖")
|
95 |
+
st.write("Ask the chatbot anything and it will provide step-by-step reasoning.")
|
96 |
+
|
97 |
+
# Input form
|
98 |
+
with st.form("chat_form", clear_on_submit=True):
|
99 |
+
user_message = st.text_input("Your Message:", key="user_input")
|
100 |
+
submitted = st.form_submit_button("Send")
|
101 |
+
|
102 |
+
# Process user input and display chat
|
103 |
+
if submitted and user_message:
|
104 |
+
# Add user message to history
|
105 |
+
st.session_state["history"].append((user_message, None))
|
106 |
+
|
107 |
+
# Display chat history
|
108 |
+
for user_text, bot_text in st.session_state["history"]:
|
109 |
+
st.markdown(f'<div class="user-message">{user_text}</div>', unsafe_allow_html=True)
|
110 |
+
if bot_text:
|
111 |
+
st.markdown(f'<div class="bot-message">{bot_text}</div>', unsafe_allow_html=True)
|
112 |
+
|
113 |
+
# Initialize Groq client
|
114 |
+
groq_client = init_groq_client()
|
115 |
+
|
116 |
+
# Generate bot response
|
117 |
+
response_placeholder = st.empty() # Placeholder for streaming updates
|
118 |
+
bot_response = ""
|
119 |
+
for partial_response in chat_with_groq(groq_client, user_message, st.session_state["history"][:-1]):
|
120 |
+
bot_response = partial_response # Update bot response incrementally
|
121 |
+
response_placeholder.markdown(f'<div class="bot-message">{bot_response}</div>', unsafe_allow_html=True)
|
122 |
+
|
123 |
+
# Update history with full bot response
|
124 |
+
st.session_state["history"][-1] = (user_message, bot_response)
|
125 |
+
|
126 |
+
# Clear chat history button
|
127 |
+
if st.button("Clear Chat"):
|
128 |
+
st.session_state["history"] = []
|
129 |
+
st.experimental_rerun()
|
130 |
+
|
131 |
+
# Footer
|
132 |
+
st.markdown("---")
|
133 |
+
st.markdown("Made with ❤️ using Groq AI")
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
huggingface_hub
|
2 |
+
streamlit
|
3 |
+
openai
|
4 |
+
groq
|