Spaces:
Running
Running
File size: 1,324 Bytes
ab964e2 5da516e df2fe71 ab964e2 fce31e7 c129502 fce31e7 df2fe71 5940d90 5da516e ab964e2 c129502 ab964e2 c129502 ab964e2 c129502 ab964e2 c129502 1c905f8 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import streamlit as st
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
import os
# Disable Streamlit usage stats to avoid permission issues
os.environ["STREAMLIT_BROWSER_GATHER_USAGE_STATS"] = "false"
# Use /tmp which is usually writable in containers
os.environ["HF_HOME"] = "/tmp/huggingface"
os.environ["TRANSFORMERS_CACHE"] = "/tmp/huggingface"
@st.cache_resource
def load_model():
# Ensure the cache directory exists
cache_dir = "/tmp/model_cache"
os.makedirs(cache_dir, exist_ok=True)
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-0.5B-Instruct", cache_dir=cache_dir)
model = AutoModelForCausalLM.from_pretrained("tianzhechu/BookQA-7B-Instruct", cache_dir=cache_dir)
return pipeline("text-generation", model=model, tokenizer=tokenizer)
st.set_page_config(page_title="LLM Demo", layout="centered")
st.title("π FLAN-T5 Small - HuggingFace Demo")
pipe = load_model()
user_input = st.text_area("Enter your instruction or question:", "")
if st.button("Generate Response"):
if user_input.strip() == "":
st.warning("Please enter some text.")
else:
with st.spinner("Generating..."):
output = pipe(user_input, max_new_tokens=100)[0]["generated_text"]
st.success("### Response:")
st.write(output) |