amaltese commited on
Commit
41787aa
·
verified ·
1 Parent(s): 656453a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +26 -19
app.py CHANGED
@@ -1,31 +1,40 @@
1
  import streamlit as st
2
- import json
3
  import requests
4
- from transformers import pipeline
5
  import wikipediaapi
 
6
 
7
- # Custom User-Agent to avoid API blocking
8
- USER_AGENT = "HistoricalChatbot/1.0 (Contact: [email protected])"
9
 
10
- # Load Wikipedia API with custom headers
11
- wiki_wiki = wikipediaapi.Wikipedia(
12
- "en",
13
- headers={"User-Agent": USER_AGENT}
14
- )
15
 
16
- # Load local AI model (Mistral-7B or Llama-2)
17
- chat_model = pipeline("text-generation", model="mistralai/Mistral-7B")
 
18
 
19
  def get_wikipedia_summary(name):
 
20
  page = wiki_wiki.page(name)
21
  return page.summary if page.exists() else "Sorry, I couldn't find information on that historical figure."
22
 
23
  def chat_with_ai(person_name, user_input):
 
24
  context = get_wikipedia_summary(person_name)
25
- prompt = f"You are {person_name}. Based on this historical information: {context}\n\nUser: {user_input}\n{person_name}:"
26
- response = chat_model(prompt, max_length=200, truncation=True)
27
- return response[0]["generated_text"].split("User:")[0].strip()
 
 
 
 
 
 
 
 
28
 
 
29
  st.title("Educational Chatbot")
30
  mode = st.sidebar.selectbox("Select Chat Mode", ["Chat with Historical Figures", "Study with a Tutor"])
31
 
@@ -54,9 +63,7 @@ if user_input and mode == "Chat with Historical Figures" and person_name:
54
  st.session_state.messages.append({"role": "assistant", "content": response})
55
  st.chat_message("assistant").write(response)
56
 
57
- # Instructions for Deployment
58
  st.sidebar.subheader("Deployment Instructions")
59
- st.sidebar.markdown("1. Ensure `transformers` and `wikipedia-api` libraries are installed.")
60
- st.sidebar.markdown("2. Rename `chatbot.py` to `app.py` for Hugging Face Spaces.")
61
- st.sidebar.markdown("3. Run `streamlit run app.py` locally.")
62
- st.sidebar.markdown("4. Deploy on [Hugging Face Spaces](https://huggingface.co/spaces) or Replit.")
 
1
  import streamlit as st
 
2
  import requests
 
3
  import wikipediaapi
4
+ import os # Import for accessing environment variables
5
 
6
+ # Fetch Hugging Face API key securely from environment variables
7
+ HUGGINGFACE_API_KEY = os.getenv("HUGGINGFACE_API_KEY")
8
 
9
+ if not HUGGINGFACE_API_KEY:
10
+ st.error("⚠️ API Key Missing! Please add your Hugging Face API key as a secret in your Space settings.")
11
+ st.stop()
 
 
12
 
13
+ # Set up Wikipedia API
14
+ USER_AGENT = "HistoricalChatbot/1.0 (Contact: your-[email protected])"
15
+ wiki_wiki = wikipediaapi.Wikipedia("en", headers={"User-Agent": USER_AGENT})
16
 
17
  def get_wikipedia_summary(name):
18
+ """Fetch summary from Wikipedia"""
19
  page = wiki_wiki.page(name)
20
  return page.summary if page.exists() else "Sorry, I couldn't find information on that historical figure."
21
 
22
  def chat_with_ai(person_name, user_input):
23
+ """Query Hugging Face API for response"""
24
  context = get_wikipedia_summary(person_name)
25
+ prompt = f"You are {person_name}. Based on this historical information: {context}\\n\\nUser: {user_input}\\n{person_name}:"
26
+
27
+ API_URL = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B"
28
+ headers = {"Authorization": f"Bearer {HUGGINGFACE_API_KEY}"}
29
+
30
+ response = requests.post(API_URL, headers=headers, json={"inputs": prompt})
31
+
32
+ if response.status_code == 200:
33
+ return response.json()[0]["generated_text"]
34
+ else:
35
+ return "I'm sorry, but I couldn't generate a response at this time."
36
 
37
+ # Streamlit UI
38
  st.title("Educational Chatbot")
39
  mode = st.sidebar.selectbox("Select Chat Mode", ["Chat with Historical Figures", "Study with a Tutor"])
40
 
 
63
  st.session_state.messages.append({"role": "assistant", "content": response})
64
  st.chat_message("assistant").write(response)
65
 
66
+ # Deployment instructions
67
  st.sidebar.subheader("Deployment Instructions")
68
+ st.sidebar.markdown("1. Add the API key as a secret in Hugging Face Spaces.")
69
+ st.sidebar.markdown("2. Run `streamlit run app.py` locally or deploy on Hugging Face Spaces.")