Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,26 +2,39 @@ import os
|
|
| 2 |
import torch
|
| 3 |
import streamlit as st
|
| 4 |
from transformers import pipeline
|
|
|
|
| 5 |
|
| 6 |
# Access Hugging Face API token from environment variables
|
| 7 |
api_key = os.getenv("llama3")
|
| 8 |
|
| 9 |
-
# Check if the token is provided (for debugging)
|
| 10 |
if not api_key:
|
| 11 |
st.error("Hugging Face API token is missing!")
|
|
|
|
| 12 |
|
| 13 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
model_id = "meta-llama/Llama-3.2-3B"
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
# Streamlit interface
|
| 23 |
-
st.title("Pirate Chatbot")
|
| 24 |
-
st.write("Ask me anything, and I'll respond in pirate speak!")
|
| 25 |
|
| 26 |
# User input
|
| 27 |
user_input = st.text_input("Your question:", "")
|
|
@@ -32,8 +45,8 @@ if user_input:
|
|
| 32 |
{"role": "user", "content": user_input},
|
| 33 |
]
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
|
|
|
| 2 |
import torch
|
| 3 |
import streamlit as st
|
| 4 |
from transformers import pipeline
|
| 5 |
+
from huggingface_hub import login
|
| 6 |
|
| 7 |
# Access Hugging Face API token from environment variables
|
| 8 |
api_key = os.getenv("llama3")
|
| 9 |
|
|
|
|
| 10 |
if not api_key:
|
| 11 |
st.error("Hugging Face API token is missing!")
|
| 12 |
+
st.stop() # Stop execution if no API key is found
|
| 13 |
|
| 14 |
+
# Authenticate with Hugging Face Hub
|
| 15 |
+
try:
|
| 16 |
+
login(api_key)
|
| 17 |
+
except Exception as e:
|
| 18 |
+
st.error(f"Authentication failed: {e}")
|
| 19 |
+
st.stop()
|
| 20 |
+
|
| 21 |
+
# Load the model
|
| 22 |
model_id = "meta-llama/Llama-3.2-3B"
|
| 23 |
+
|
| 24 |
+
try:
|
| 25 |
+
pipe = pipeline(
|
| 26 |
+
"text-generation",
|
| 27 |
+
model=model_id,
|
| 28 |
+
torch_dtype=torch.bfloat16,
|
| 29 |
+
device_map="auto",
|
| 30 |
+
)
|
| 31 |
+
except Exception as e:
|
| 32 |
+
st.error(f"Error loading model: {e}")
|
| 33 |
+
st.stop()
|
| 34 |
|
| 35 |
# Streamlit interface
|
| 36 |
+
st.title("Pirate Chatbot 🏴☠️")
|
| 37 |
+
st.write("Ahoy, matey! Ask me anything, and I'll respond in pirate speak! ☠️⚓")
|
| 38 |
|
| 39 |
# User input
|
| 40 |
user_input = st.text_input("Your question:", "")
|
|
|
|
| 45 |
{"role": "user", "content": user_input},
|
| 46 |
]
|
| 47 |
|
| 48 |
+
try:
|
| 49 |
+
outputs = pipe(messages, max_new_tokens=256)
|
| 50 |
+
st.write(outputs[0]["generated_text"][-1])
|
| 51 |
+
except Exception as e:
|
| 52 |
+
st.error(f"Error generating response: {e}")
|