CCockrum commited on
Commit
d6f5773
·
verified ·
1 Parent(s): ab0a991

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -158
app.py CHANGED
@@ -1,200 +1,83 @@
1
  import os
2
- import requests
3
- import streamlit as st
4
  from langchain_huggingface import HuggingFaceEndpoint
 
5
  from langchain_core.prompts import PromptTemplate
6
  from langchain_core.output_parsers import StrOutputParser
7
- from transformers import pipeline
8
-
9
- # Use environment variables for keys
10
- HF_TOKEN = os.getenv("HF_TOKEN")
11
- if HF_TOKEN is None:
12
- raise ValueError("HF_TOKEN environment variable not set. Please set it in your Hugging Face Space settings.")
13
-
14
- NASA_API_KEY = os.getenv("NASA_API_KEY")
15
- if NASA_API_KEY is None:
16
- raise ValueError("NASA_API_KEY environment variable not set. Please set it in your Hugging Face Space settings.")
17
-
18
- # Set up Streamlit UI
19
- st.set_page_config(page_title="HAL - NASA ChatBot", page_icon="🚀")
20
-
21
- # --- Initialize Session State Variables ---
22
- if "chat_history" not in st.session_state:
23
- # Initial greeting stored in chat history
24
- st.session_state.chat_history = [{"role": "assistant", "content": "Hello! How can I assist you today?"}]
25
-
26
- if "response_ready" not in st.session_state:
27
- st.session_state.response_ready = False # Tracks whether HAL has responded
28
-
29
- if "follow_up" not in st.session_state:
30
- st.session_state.follow_up = "" # Stores follow-up question
31
 
32
- # --- Set Up Model & API Functions ---
33
  model_id = "mistralai/Mistral-7B-Instruct-v0.3"
34
 
35
- # Initialize sentiment analysis pipeline with explicit model specification
36
- sentiment_analyzer = pipeline(
37
- "sentiment-analysis",
38
- model="distilbert/distilbert-base-uncased-finetuned-sst-2-english",
39
- revision="714eb0f"
40
- )
41
-
42
- def get_llm_hf_inference(model_id=model_id, max_new_tokens=128, temperature=0.7):
43
- # Specify task="text-generation" so that the endpoint uses the right model function.
44
- return HuggingFaceEndpoint(
45
  repo_id=model_id,
46
  max_new_tokens=max_new_tokens,
47
  temperature=temperature,
48
- token=HF_TOKEN,
49
- task="text-generation"
50
  )
 
51
 
52
  def get_nasa_apod():
 
 
 
53
  url = f"https://api.nasa.gov/planetary/apod?api_key={NASA_API_KEY}"
54
  response = requests.get(url)
55
  if response.status_code == 200:
56
  data = response.json()
57
- return data.get("url", ""), data.get("title", ""), data.get("explanation", "")
58
  else:
59
- return "", "NASA Data Unavailable", "I couldn't fetch data from NASA right now. Please try again later."
60
 
61
- def analyze_sentiment(user_text):
62
- result = sentiment_analyzer(user_text)[0]
63
- return result['label']
64
-
65
- def predict_action(user_text):
66
  if "NASA" in user_text or "space" in user_text:
67
- return "nasa_info"
68
- return "general_query"
69
-
70
- def generate_follow_up(user_text):
71
- """
72
- Generates a concise and conversational follow-up question related to the user's input.
73
- """
74
- prompt_text = (
75
- f"Given the user's question: '{user_text}', generate a SHORT and SIMPLE follow-up question. "
76
- "Make it conversational and friendly. Example: 'Would you like to learn more about the six types of quarks?' "
77
- "Do NOT provide long explanations—just ask a friendly follow-up question."
78
- )
79
- hf = get_llm_hf_inference(max_new_tokens=32, temperature=0.7)
80
- return hf.invoke(input=prompt_text).strip()
81
-
82
- def get_response(system_message, chat_history, user_text, max_new_tokens=256):
83
- """
84
- Generates HAL's response in a friendly, conversational manner.
85
- The prompt instructs the model to ignore previous greetings and focus on the new user question.
86
- """
87
- sentiment = analyze_sentiment(user_text)
88
- action = predict_action(user_text)
89
-
90
- if action == "nasa_info":
91
- nasa_url, nasa_title, nasa_explanation = get_nasa_apod()
92
- response = f"**{nasa_title}**\n\n{nasa_explanation}"
93
  chat_history.append({'role': 'user', 'content': user_text})
94
- chat_history.append({'role': 'assistant', 'content': response})
95
-
96
- follow_up = generate_follow_up(user_text)
97
- chat_history.append({'role': 'assistant', 'content': follow_up})
98
- return response, follow_up, chat_history, nasa_url
99
 
100
- hf = get_llm_hf_inference(max_new_tokens=max_new_tokens, temperature=0.9)
101
 
102
- # Updated prompt: Instruct the model not to repeat previous greetings.
103
  prompt = PromptTemplate.from_template(
104
  (
105
- "[INST] {system_message}\n\nCurrent Conversation:\n{chat_history}\n\n"
106
- "User: {user_text}.\n [/INST]\n"
107
- "AI: Please answer the user's question without repeating any previous greetings. "
108
- "Keep your response friendly and conversational, starting with a phrase like "
109
- "'Certainly!', 'Of course!', or 'Great question!'.\nHAL:"
110
  )
111
  )
112
  chat = prompt | hf.bind(skip_prompt=True) | StrOutputParser(output_key='content')
113
  response = chat.invoke(input=dict(system_message=system_message, user_text=user_text, chat_history=chat_history))
114
- response = response.split("HAL:")[-1].strip()
115
 
116
  chat_history.append({'role': 'user', 'content': user_text})
117
  chat_history.append({'role': 'assistant', 'content': response})
 
118
 
119
- if sentiment == "NEGATIVE":
120
- response = "I'm here to help. Let me know what I can do for you. 😊"
 
 
121
 
122
- follow_up = generate_follow_up(user_text)
123
- chat_history.append({'role': 'assistant', 'content': follow_up})
124
-
125
- return response, follow_up, chat_history, None
126
-
127
- # --- Chat UI ---
128
- st.title("🚀 HAL - Your NASA AI Assistant")
129
- st.markdown("🌌 *Ask me about space, NASA, and beyond!*")
130
-
131
- # Sidebar: Reset Chat
132
  if st.sidebar.button("Reset Chat"):
133
  st.session_state.chat_history = [{"role": "assistant", "content": "Hello! How can I assist you today?"}]
134
- st.session_state.response_ready = False
135
- st.session_state.follow_up = ""
136
- st.experimental_rerun()
137
-
138
- # Custom Chat Styling
139
- st.markdown("""
140
- <style>
141
- .user-msg {
142
- background-color: #0078D7;
143
- color: white;
144
- padding: 10px;
145
- border-radius: 10px;
146
- margin-bottom: 5px;
147
- width: fit-content;
148
- max-width: 80%;
149
- }
150
- .assistant-msg {
151
- background-color: #333333;
152
- color: white;
153
- padding: 10px;
154
- border-radius: 10px;
155
- margin-bottom: 5px;
156
- width: fit-content;
157
- max-width: 80%;
158
- }
159
- .container {
160
- display: flex;
161
- flex-direction: column;
162
- align-items: flex-start;
163
- }
164
- @media (max-width: 600px) {
165
- .user-msg, .assistant-msg { font-size: 16px; max-width: 100%; }
166
- }
167
- </style>
168
- """, unsafe_allow_html=True)
169
-
170
- # Chat History Display
171
- st.markdown("<div class='container'>", unsafe_allow_html=True)
172
- for message in st.session_state.chat_history:
173
- if message["role"] == "user":
174
- st.markdown(f"<div class='user-msg'><strong>You:</strong> {message['content']}</div>", unsafe_allow_html=True)
175
- else:
176
- st.markdown(f"<div class='assistant-msg'><strong>HAL:</strong> {message['content']}</div>", unsafe_allow_html=True)
177
- st.markdown("</div>", unsafe_allow_html=True)
178
-
179
- # --- Single Input Box for Both Initial and Follow-Up Messages ---
180
- user_input = st.chat_input("Type your message here...") # Only ONE chat_input()
181
 
 
 
182
  if user_input:
183
- response, follow_up, st.session_state.chat_history, image_url = get_response(
184
  system_message="You are a helpful AI assistant.",
185
  user_text=user_input,
186
- chat_history=st.session_state.chat_history
 
187
  )
188
-
189
- st.markdown(f"<div class='assistant-msg'><strong>HAL:</strong> {response}</div>", unsafe_allow_html=True)
190
-
191
- if image_url:
192
- st.image(image_url, caption="NASA Image of the Day")
193
-
194
- st.session_state.follow_up = follow_up
195
- st.session_state.response_ready = True
196
-
197
- if st.session_state.response_ready and st.session_state.follow_up:
198
- st.markdown(f"<div class='assistant-msg'><strong>HAL:</strong> {st.session_state.follow_up}</div>", unsafe_allow_html=True)
199
- st.session_state.response_ready = False
200
 
 
1
  import os
 
 
2
  from langchain_huggingface import HuggingFaceEndpoint
3
+ import streamlit as st
4
  from langchain_core.prompts import PromptTemplate
5
  from langchain_core.output_parsers import StrOutputParser
6
+ import requests
7
+ from config import NASA_API_KEY # Import the NASA API key from the configuration file
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
 
9
  model_id = "mistralai/Mistral-7B-Instruct-v0.3"
10
 
11
+ def get_llm_hf_inference(model_id=model_id, max_new_tokens=128, temperature=0.1):
12
+ llm = HuggingFaceEndpoint(
 
 
 
 
 
 
 
 
13
  repo_id=model_id,
14
  max_new_tokens=max_new_tokens,
15
  temperature=temperature,
16
+ token=os.getenv("HF_TOKEN") # Hugging Face token from environment variable
 
17
  )
18
+ return llm
19
 
20
  def get_nasa_apod():
21
+ """
22
+ Fetch the Astronomy Picture of the Day (APOD) from the NASA API.
23
+ """
24
  url = f"https://api.nasa.gov/planetary/apod?api_key={NASA_API_KEY}"
25
  response = requests.get(url)
26
  if response.status_code == 200:
27
  data = response.json()
28
+ return f"Title: {data['title']}\nExplanation: {data['explanation']}\nURL: {data['url']}"
29
  else:
30
+ return "I couldn't fetch data from NASA right now. Please try again later."
31
 
32
+ def get_response(system_message, chat_history, user_text,
33
+ eos_token_id=['User'], max_new_tokens=256, get_llm_hf_kws={}):
 
 
 
34
  if "NASA" in user_text or "space" in user_text:
35
+ nasa_response = get_nasa_apod()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36
  chat_history.append({'role': 'user', 'content': user_text})
37
+ chat_history.append({'role': 'assistant', 'content': nasa_response})
38
+ return nasa_response, chat_history
 
 
 
39
 
40
+ hf = get_llm_hf_inference(max_new_tokens=max_new_tokens, temperature=0.1)
41
 
 
42
  prompt = PromptTemplate.from_template(
43
  (
44
+ "[INST] {system_message}"
45
+ "\nCurrent Conversation:\n{chat_history}\n\n"
46
+ "\nUser: {user_text}.\n [/INST]"
47
+ "\nAI:"
 
48
  )
49
  )
50
  chat = prompt | hf.bind(skip_prompt=True) | StrOutputParser(output_key='content')
51
  response = chat.invoke(input=dict(system_message=system_message, user_text=user_text, chat_history=chat_history))
52
+ response = response.split("AI:")[-1]
53
 
54
  chat_history.append({'role': 'user', 'content': user_text})
55
  chat_history.append({'role': 'assistant', 'content': response})
56
+ return response, chat_history
57
 
58
+ # Streamlit setup
59
+ st.set_page_config(page_title="HuggingFace ChatBot", page_icon="🤗")
60
+ st.title("NASA Personal Assistant")
61
+ st.markdown(f"*This chatbot uses {model_id} and NASA's APIs to provide information and responses.*")
62
 
63
+ # Initialize session state
64
+ if "chat_history" not in st.session_state:
65
+ st.session_state.chat_history = [{"role": "assistant", "content": "Hello! How can I assist you today?"}]
66
+
67
+ # Sidebar for settings
 
 
 
 
 
68
  if st.sidebar.button("Reset Chat"):
69
  st.session_state.chat_history = [{"role": "assistant", "content": "Hello! How can I assist you today?"}]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70
 
71
+ # Main chat interface
72
+ user_input = st.chat_input(placeholder="Type your message here...")
73
  if user_input:
74
+ response, st.session_state.chat_history = get_response(
75
  system_message="You are a helpful AI assistant.",
76
  user_text=user_input,
77
+ chat_history=st.session_state.chat_history,
78
+ max_new_tokens=128
79
  )
80
+ # Display messages
81
+ for message in st.session_state.chat_history:
82
+ st.chat_message(message["role"]).write(message["content"])
 
 
 
 
 
 
 
 
 
83