amaltese commited on
Commit
9ed30a6
·
verified ·
1 Parent(s): 55a936b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -27
app.py CHANGED
@@ -1,40 +1,25 @@
1
  import streamlit as st
 
2
  import requests
 
3
  import wikipediaapi
4
- import os # For accessing environment variables
5
 
6
- # Securely fetch Hugging Face API key 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: [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-Instruct-v0.1"
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,7 +48,9 @@ if user_input and mode == "Chat with Historical Figures" and person_name:
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.")
 
 
 
1
  import streamlit as st
2
+ import json
3
  import requests
4
+ from transformers import pipeline
5
  import wikipediaapi
 
6
 
7
+ # Load historical figures and tutor topics from Wikipedia dynamically
8
+ wiki_wiki = wikipediaapi.Wikipedia("en")
9
 
10
+ # Load local AI model (Mistral-7B or Llama-2)
11
+ chat_model = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.1")
 
 
 
 
 
12
 
13
  def get_wikipedia_summary(name):
 
14
  page = wiki_wiki.page(name)
15
  return page.summary if page.exists() else "Sorry, I couldn't find information on that historical figure."
16
 
17
  def chat_with_ai(person_name, user_input):
 
18
  context = get_wikipedia_summary(person_name)
19
+ prompt = f"You are {person_name}. Based on this historical information: {context}\n\nUser: {user_input}\n{person_name}:"
20
+ response = chat_model(prompt, max_length=200, truncation=True)
21
+ return response[0]["generated_text"].split("User:")[0].strip()
 
 
 
 
 
 
 
 
22
 
 
23
  st.title("Educational Chatbot")
24
  mode = st.sidebar.selectbox("Select Chat Mode", ["Chat with Historical Figures", "Study with a Tutor"])
25
 
 
48
  st.session_state.messages.append({"role": "assistant", "content": response})
49
  st.chat_message("assistant").write(response)
50
 
51
+ # Instructions for Deployment
52
  st.sidebar.subheader("Deployment Instructions")
53
+ st.sidebar.markdown("1. Ensure `transformers` and `wikipedia-api` libraries are installed.")
54
+ st.sidebar.markdown("2. Rename `chatbot.py` to `app.py` for Hugging Face Spaces.")
55
+ st.sidebar.markdown("3. Run `streamlit run app.py` locally.")
56
+ st.sidebar.markdown("4. Deploy on [Hugging Face Spaces](https://huggingface.co/spaces) or Replit.")