Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,15 @@
|
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
from langchain import HuggingFaceHub
|
| 3 |
-
from dotenv import load_dotenv
|
| 4 |
-
import os
|
| 5 |
-
|
| 6 |
|
|
|
|
| 7 |
load_dotenv()
|
| 8 |
|
| 9 |
# Set your Hugging Face API token from the environment variable
|
| 10 |
HUGGINGFACE_API_TOKEN = os.getenv("HUGGINGFACE_API_TOKEN")
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
# Function to return the response from Hugging Face model
|
| 15 |
def load_answer(question):
|
| 16 |
try:
|
| 17 |
# Initialize the Hugging Face model using LangChain's HuggingFaceHub class
|
|
@@ -21,16 +19,16 @@ def load_answer(question):
|
|
| 21 |
model_kwargs={"temperature": 0} # Optional: Control response randomness
|
| 22 |
)
|
| 23 |
|
| 24 |
-
# Call the model with the user's question and get the response
|
| 25 |
-
answer = llm.
|
| 26 |
return answer
|
| 27 |
except Exception as e:
|
| 28 |
# Capture and return any exceptions or errors
|
| 29 |
return f"Error: {str(e)}"
|
| 30 |
|
| 31 |
# Streamlit App UI starts here
|
| 32 |
-
st.set_page_config(page_title="
|
| 33 |
-
st.header("
|
| 34 |
|
| 35 |
# Function to get user input
|
| 36 |
def get_text():
|
|
@@ -43,7 +41,7 @@ user_input = get_text()
|
|
| 43 |
# Create a button for generating the response
|
| 44 |
submit = st.button('Generate')
|
| 45 |
|
| 46 |
-
# If generate button is clicked and user input is not empty
|
| 47 |
if submit and user_input:
|
| 48 |
response = load_answer(user_input)
|
| 49 |
st.subheader("Answer:")
|
|
|
|
| 1 |
+
import os
|
| 2 |
import streamlit as st
|
| 3 |
+
from dotenv import load_dotenv # Importing load_dotenv to load environment variables
|
| 4 |
from langchain import HuggingFaceHub
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
# Load environment variables from the .env file
|
| 7 |
load_dotenv()
|
| 8 |
|
| 9 |
# Set your Hugging Face API token from the environment variable
|
| 10 |
HUGGINGFACE_API_TOKEN = os.getenv("HUGGINGFACE_API_TOKEN")
|
| 11 |
|
| 12 |
+
# Function to return the response from the Hugging Face model
|
|
|
|
|
|
|
| 13 |
def load_answer(question):
|
| 14 |
try:
|
| 15 |
# Initialize the Hugging Face model using LangChain's HuggingFaceHub class
|
|
|
|
| 19 |
model_kwargs={"temperature": 0} # Optional: Control response randomness
|
| 20 |
)
|
| 21 |
|
| 22 |
+
# Call the model with the user's question and get the response using .predict()
|
| 23 |
+
answer = llm.predict(question)
|
| 24 |
return answer
|
| 25 |
except Exception as e:
|
| 26 |
# Capture and return any exceptions or errors
|
| 27 |
return f"Error: {str(e)}"
|
| 28 |
|
| 29 |
# Streamlit App UI starts here
|
| 30 |
+
st.set_page_config(page_title="Hugging Face Demo", page_icon=":robot:")
|
| 31 |
+
st.header("Hugging Face Demo")
|
| 32 |
|
| 33 |
# Function to get user input
|
| 34 |
def get_text():
|
|
|
|
| 41 |
# Create a button for generating the response
|
| 42 |
submit = st.button('Generate')
|
| 43 |
|
| 44 |
+
# If the generate button is clicked and user input is not empty
|
| 45 |
if submit and user_input:
|
| 46 |
response = load_answer(user_input)
|
| 47 |
st.subheader("Answer:")
|