Spaces:
Sleeping
Sleeping
File size: 1,089 Bytes
4ce15d7 8b8242c 4ce15d7 32414d2 4ce15d7 fc92fad 4ce15d7 bde561e 8b8242c 4ce15d7 fc92fad 4ce15d7 8b8242c |
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 |
import os
import streamlit as st
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
from huggingface_hub import login
# Load Hugging Face Token from Secrets
hf_token = os.getenv("HF_TOKEN")
if not hf_token:
st.error("Hugging Face token is missing! Please add it to Hugging Face Secrets.")
st.stop()
# Authenticate
login(token=hf_token)
# Load the model and tokenizer with authentication
MODEL_NAME = "google/gemma-2b-it"
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME, token=hf_token)
model = AutoModelForCausalLM.from_pretrained(MODEL_NAME, token=hf_token, torch_dtype=torch.float16, device_map="auto")
# Streamlit UI
st.title("Gemma-2B Code Assistant")
user_input = st.text_area("Enter your coding query:")
if st.button("Generate Code"):
if user_input:
inputs = tokenizer(user_input, return_tensors="pt").to("cuda")
output = model.generate(**inputs, max_new_tokens=100)
response = tokenizer.decode(output[0], skip_special_tokens=True)
st.write(response)
else:
st.warning("Please enter a query!")
|