CCockrum commited on
Commit
fc42bd4
·
verified ·
1 Parent(s): 5c095c6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -9
app.py CHANGED
@@ -5,13 +5,22 @@ 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
- from config import NASA_API_KEY # Ensure this file exists with your NASA API Key
 
 
 
 
 
 
 
 
9
 
10
  # Set up Streamlit UI
11
  st.set_page_config(page_title="HAL - NASA ChatBot", page_icon="🚀")
12
 
13
- # --- Ensure Session State Variables are Initialized ---
14
  if "chat_history" not in st.session_state:
 
15
  st.session_state.chat_history = [{"role": "assistant", "content": "Hello! How can I assist you today?"}]
16
 
17
  if "response_ready" not in st.session_state:
@@ -31,12 +40,12 @@ sentiment_analyzer = pipeline(
31
  )
32
 
33
  def get_llm_hf_inference(model_id=model_id, max_new_tokens=128, temperature=0.7):
34
- # Explicitly specify task="text-generation" so that the endpoint knows which task to run
35
  return HuggingFaceEndpoint(
36
  repo_id=model_id,
37
  max_new_tokens=max_new_tokens,
38
  temperature=temperature,
39
- token=os.getenv("HF_TOKEN"),
40
  task="text-generation"
41
  )
42
 
@@ -72,7 +81,8 @@ def generate_follow_up(user_text):
72
 
73
  def get_response(system_message, chat_history, user_text, max_new_tokens=256):
74
  """
75
- Generates HAL's response, making it more conversational and engaging.
 
76
  """
77
  sentiment = analyze_sentiment(user_text)
78
  action = predict_action(user_text)
@@ -89,12 +99,14 @@ def get_response(system_message, chat_history, user_text, max_new_tokens=256):
89
 
90
  hf = get_llm_hf_inference(max_new_tokens=max_new_tokens, temperature=0.9)
91
 
 
92
  prompt = PromptTemplate.from_template(
93
  (
94
- "[INST] {system_message}\n\nCurrent Conversation:\n{chat_history}\n\nUser: {user_text}.\n [/INST]\n"
95
- "AI: Keep responses conversational and engaging. Start with a friendly phrase like "
96
- "'Certainly!', 'Of course!', or 'Great question!' before answering. "
97
- "Keep responses concise but engaging.\nHAL:"
 
98
  )
99
  )
100
  chat = prompt | hf.bind(skip_prompt=True) | StrOutputParser(output_key='content')
 
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:
 
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
 
 
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)
 
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')