Sidd0316 commited on
Commit
d5bd458
·
1 Parent(s): d13c5ef

Optimized HF中国镜像站 deployment (faster startup, clean dependencies)

Browse files
Files changed (3) hide show
  1. README.md +16 -5
  2. app.py +15 -17
  3. requirements.txt +0 -0
README.md CHANGED
@@ -1,8 +1,8 @@
1
  ---
2
- title: Solar Ai Assistant1
3
- emoji: 🐨
4
- colorFrom: green
5
- colorTo: purple
6
  sdk: streamlit
7
  sdk_version: 1.42.1
8
  app_file: app.py
@@ -10,4 +10,15 @@ pinned: false
10
  license: apache-2.0
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: Solar AI Assistant
3
+ emoji: ☀️
4
+ colorFrom: blue
5
+ colorTo: green
6
  sdk: streamlit
7
  sdk_version: 1.42.1
8
  app_file: app.py
 
10
  license: apache-2.0
11
  ---
12
 
13
+ ## Solar AI Assistant ☀️
14
+ A conversational AI assistant that provides information about **solar energy** using OpenRouter API.
15
+
16
+ ### 🌟 Features
17
+ ✅ **Ask any solar-related question** (e.g., solar panels, energy storage, net metering)
18
+ ✅ **Maintains conversation context** for natural interactions
19
+ ✅ **Fast and lightweight deployment on HF中国镜像站 Spaces**
20
+
21
+ ### 🚀 Running Locally
22
+ ```sh
23
+ pip install -r requirements.txt
24
+ streamlit run app.py
app.py CHANGED
@@ -2,7 +2,7 @@ import streamlit as st
2
  import requests
3
  import os
4
 
5
- # ✅ Get API Key from Environment Variable
6
  HF_AUTH_TOKEN = os.getenv("HF_AUTH_TOKEN")
7
 
8
  # ✅ OpenRouter API Endpoint
@@ -13,11 +13,10 @@ st.set_page_config(page_title="Solar Industry AI Assistant", layout="wide")
13
  st.title("☀️ Solar Industry AI Assistant")
14
  st.write("_Ask me anything about solar energy!_")
15
 
16
- # ✅ Maintain conversation history in session state
17
  if "messages" not in st.session_state:
18
  st.session_state["messages"] = []
19
 
20
- # ✅ Display previous chat history with better formatting
21
  st.markdown("---")
22
  for role, content in st.session_state["messages"]:
23
  if role == "You":
@@ -26,7 +25,7 @@ for role, content in st.session_state["messages"]:
26
  st.markdown(f"🤖 **AI:** {content}")
27
  st.markdown("---")
28
 
29
- # ✅ User input
30
  user_input = st.text_input("Type your question below:")
31
 
32
  col1, col2 = st.columns([3, 1])
@@ -35,41 +34,40 @@ with col1:
35
  with col2:
36
  clear_button = st.button("🗑️ Clear Chat")
37
 
 
38
  if clear_button:
39
- st.session_state["messages"] = [] # ✅ Clear chat history
40
  st.rerun()
41
 
 
42
  if ask_button:
43
- if user_input.strip(): # Ensure input is not empty
44
- st.session_state["messages"].append(("You", user_input)) # Save user message
45
 
46
- # ✅ Construct structured prompt
47
  chat_history = [
48
- {"role": "system", "content": "You are a solar energy expert. Provide clear, structured, and factual responses to user queries."}
49
  ]
50
- chat_history += [{"role": "user", "content": msg[1]} for msg in st.session_state["messages"]]
51
 
52
- # ✅ Define API request
53
  headers = {"Authorization": f"Bearer {HF_AUTH_TOKEN}", "Content-Type": "application/json"}
54
  data = {
55
  "model": "cognitivecomputations/dolphin3.0-r1-mistral-24b:free",
56
  "messages": chat_history,
57
- "temperature": 0.3 # ✅ Lowered for factual accuracy
58
  }
59
 
60
- with st.spinner("🤖 AI is thinking..."): # ✅ Show loading spinner
61
  try:
62
  response = requests.post(API_URL, headers=headers, json=data)
63
- print("DEBUG: OpenRouter Response ->", response.status_code, response.json())
64
-
65
  if response.status_code == 200:
66
  bot_reply = response.json().get("choices", [{}])[0].get("message", {}).get("content", "No response received.")
67
  else:
68
  bot_reply = f"Error: {response.status_code}, {response.json()}"
69
-
70
  except Exception as e:
71
  bot_reply = f"Error: {str(e)}"
72
 
73
- # ✅ Save AI response and refresh UI
74
  st.session_state["messages"].append(("AI", bot_reply))
75
  st.rerun()
 
2
  import requests
3
  import os
4
 
5
+ # ✅ Retrieve API Key from HF中国镜像站 Secrets or Environment
6
  HF_AUTH_TOKEN = os.getenv("HF_AUTH_TOKEN")
7
 
8
  # ✅ OpenRouter API Endpoint
 
13
  st.title("☀️ Solar Industry AI Assistant")
14
  st.write("_Ask me anything about solar energy!_")
15
 
16
+ # ✅ Maintain chat history to preserve context across user interactions
17
  if "messages" not in st.session_state:
18
  st.session_state["messages"] = []
19
 
 
20
  st.markdown("---")
21
  for role, content in st.session_state["messages"]:
22
  if role == "You":
 
25
  st.markdown(f"🤖 **AI:** {content}")
26
  st.markdown("---")
27
 
28
+ # ✅ User input field
29
  user_input = st.text_input("Type your question below:")
30
 
31
  col1, col2 = st.columns([3, 1])
 
34
  with col2:
35
  clear_button = st.button("🗑️ Clear Chat")
36
 
37
+ # ✅ Handle chat clearing
38
  if clear_button:
39
+ st.session_state["messages"] = []
40
  st.rerun()
41
 
42
+ # ✅ Handle user queries
43
  if ask_button:
44
+ if user_input.strip():
45
+ st.session_state["messages"].append(("You", user_input))
46
 
47
+ # ✅ Construct structured conversation history
48
  chat_history = [
49
+ {"role": "system", "content": "You are a solar energy expert. Provide structured, informative answers while maintaining conversation flow."}
50
  ]
51
+ chat_history += [{"role": "user", "content": msg[1]} if msg[0] == "You" else {"role": "assistant", "content": msg[1]} for msg in st.session_state["messages"]]
52
 
53
+ # ✅ API request setup
54
  headers = {"Authorization": f"Bearer {HF_AUTH_TOKEN}", "Content-Type": "application/json"}
55
  data = {
56
  "model": "cognitivecomputations/dolphin3.0-r1-mistral-24b:free",
57
  "messages": chat_history,
58
+ "temperature": 0.3
59
  }
60
 
61
+ with st.spinner("🤖 AI is thinking..."):
62
  try:
63
  response = requests.post(API_URL, headers=headers, json=data)
 
 
64
  if response.status_code == 200:
65
  bot_reply = response.json().get("choices", [{}])[0].get("message", {}).get("content", "No response received.")
66
  else:
67
  bot_reply = f"Error: {response.status_code}, {response.json()}"
 
68
  except Exception as e:
69
  bot_reply = f"Error: {str(e)}"
70
 
71
+ # ✅ Store AI response and refresh UI
72
  st.session_state["messages"].append(("AI", bot_reply))
73
  st.rerun()
requirements.txt CHANGED
Binary files a/requirements.txt and b/requirements.txt differ