Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
+
from dotenv import load_dotenv
|
4 |
+
import os
|
5 |
+
|
6 |
+
# Load .env file
|
7 |
+
load_dotenv()
|
8 |
+
|
9 |
+
# Get API key from environment variable
|
10 |
+
api_key = os.getenv("HUGGINGFACEHUB_API_TOKEN")
|
11 |
+
|
12 |
+
st.set_page_config(page_title="Intellicounsel AI Chat", page_icon="🤖")
|
13 |
+
|
14 |
+
# Add system prompt once at the start
|
15 |
+
system_prompt = {
|
16 |
+
"role": "system",
|
17 |
+
"content": (
|
18 |
+
"You are Intellicounsel, an intelligent and friendly AI advisor that helps students "
|
19 |
+
"with college applications, SOP reviews, resume tips, and academic advice. "
|
20 |
+
"Respond clearly and helpfully, always tailored to the student's needs."
|
21 |
+
)
|
22 |
+
}
|
23 |
+
|
24 |
+
# Initialize chat history
|
25 |
+
if "messages" not in st.session_state:
|
26 |
+
st.session_state.messages = []
|
27 |
+
|
28 |
+
# Title
|
29 |
+
st.title("🧠 Intellicounsel v2 — AI Chat Advisor")
|
30 |
+
|
31 |
+
# User input
|
32 |
+
user_input = st.chat_input("Ask something like SOP tips or university suggestions...")
|
33 |
+
|
34 |
+
# Show past messages
|
35 |
+
for msg in st.session_state.messages:
|
36 |
+
with st.chat_message(msg["role"]):
|
37 |
+
st.markdown(msg["content"])
|
38 |
+
|
39 |
+
# If new input
|
40 |
+
if user_input:
|
41 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
42 |
+
|
43 |
+
with st.chat_message("user"):
|
44 |
+
st.markdown(user_input)
|
45 |
+
|
46 |
+
with st.chat_message("assistant"):
|
47 |
+
with st.spinner("Thinking..."):
|
48 |
+
try:
|
49 |
+
client = InferenceClient(
|
50 |
+
model="nvidia/Llama-3_1-Nemotron-Ultra-253B-v1",
|
51 |
+
provider="nebius",
|
52 |
+
api_key=api_key,
|
53 |
+
)
|
54 |
+
|
55 |
+
# Add system prompt only once at the start of the context
|
56 |
+
full_context = [system_prompt] + st.session_state.messages
|
57 |
+
|
58 |
+
completion = client.chat.completions.create(
|
59 |
+
model="nvidia/Llama-3_1-Nemotron-Ultra-253B-v1",
|
60 |
+
messages=full_context,
|
61 |
+
max_tokens=2048,
|
62 |
+
)
|
63 |
+
|
64 |
+
response = completion.choices[0].message.content
|
65 |
+
|
66 |
+
except Exception as e:
|
67 |
+
response = f"❌ Error: {e}"
|
68 |
+
|
69 |
+
st.markdown(response)
|
70 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|