vismaya2939 commited on
Commit
2e7838e
1 Parent(s): e9619e3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -0
app.py ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
16
+ llm = HuggingFaceHub(
17
+ repo_id="mistralai/Mistral-7B-Instruct-v0.3", # Hugging Face model repo
18
+ huggingfacehub_api_token=HUGGINGFACE_API_TOKEN, # Pass your API token
19
+ model_kwargs={"temperature": 0.1} # Set a strictly positive temperature
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():
35
+ input_text = st.text_input("You: ", key="input")
36
+ return input_text
37
+
38
+ # Get user input
39
+ user_input = get_text()
40
+
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:")
48
+ st.write(response)
49
+ elif submit:
50
+ st.warning("Please enter a question.") # Warning for empty input