Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,39 @@
|
|
1 |
import streamlit as st
|
2 |
-
from
|
3 |
-
import
|
4 |
-
import torch
|
5 |
|
6 |
-
#
|
7 |
-
|
8 |
-
def load_model():
|
9 |
-
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen-7B", trust_remote_code=True)
|
10 |
-
model = AutoModelForCausalLM.from_pretrained(
|
11 |
-
"Qwen/Qwen-7B",
|
12 |
-
load_in_4bit=True,
|
13 |
-
device_map="auto",
|
14 |
-
trust_remote_code=True # Allows custom code execution
|
15 |
-
)
|
16 |
-
return tokenizer, model
|
17 |
|
18 |
-
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
-
#
|
24 |
-
|
25 |
|
26 |
-
#
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
from huggingface_hub import InferenceClient
|
3 |
+
import os
|
|
|
4 |
|
5 |
+
# Retrieve the Hugging Face token from environment variables
|
6 |
+
hf_token = os.getenv("HF_API_TOKEN")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
+
# Initialize the Hugging Face Inference Client
|
9 |
+
client = InferenceClient(
|
10 |
+
model="Qwen/Qwen2-7B",
|
11 |
+
token=hf_token,
|
12 |
+
)
|
13 |
|
14 |
+
def get_chat_completion(message):
|
15 |
+
try:
|
16 |
+
response = client.chat_completion(
|
17 |
+
messages=[{"role": "user", "content": message}],
|
18 |
+
max_tokens=500,
|
19 |
+
stream=False,
|
20 |
+
)
|
21 |
+
completion = response[0]['choices'][0]['message']['content']
|
22 |
+
return completion
|
23 |
+
except Exception as e:
|
24 |
+
return f"Error: {e}"
|
25 |
|
26 |
+
# Streamlit app layout
|
27 |
+
st.title("Chat with Hugging Face Model")
|
28 |
|
29 |
+
# Input from the user
|
30 |
+
user_input = st.text_input("Enter your message:")
|
31 |
+
|
32 |
+
if st.button("Send"):
|
33 |
+
if user_input:
|
34 |
+
# Get response from the model
|
35 |
+
response = get_chat_completion(user_input)
|
36 |
+
st.write("**Response:**")
|
37 |
+
st.write(response)
|
38 |
+
else:
|
39 |
+
st.write("Please enter a message.")
|