Spaces:
Sleeping
Sleeping
Add files via upload
Browse files- app.py +35 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from huggingface_hub import login
|
| 4 |
+
import torch
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# Set page configuration
|
| 8 |
+
st.set_page_config(page_title="Text GenAI Model", page_icon="🤖")
|
| 9 |
+
st.title("Text GenAI Model")
|
| 10 |
+
st.subheader("Answer Random Questions Using Hugging Face Models")
|
| 11 |
+
|
| 12 |
+
# Fetch Hugging Face token from Streamlit Secrets
|
| 13 |
+
access_token_read = st.secrets["HUGGINGFACE_TOKEN"] # Ensure this is set in your Streamlit Cloud Secrets
|
| 14 |
+
|
| 15 |
+
# Free up GPU memory (if using GPU)
|
| 16 |
+
torch.cuda.empty_cache()
|
| 17 |
+
|
| 18 |
+
# Set environment variable to avoid fragmentation
|
| 19 |
+
os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
|
| 20 |
+
|
| 21 |
+
# Login to Hugging Face Hub using the access token
|
| 22 |
+
login(token=access_token_read)
|
| 23 |
+
|
| 24 |
+
# Initialize the text generation pipeline with GPT-2 model
|
| 25 |
+
pipe = pipeline("text-generation", model="gpt2", device=-1) # Using CPU
|
| 26 |
+
|
| 27 |
+
# Input from the user
|
| 28 |
+
text = st.text_input("Ask a Random Question")
|
| 29 |
+
|
| 30 |
+
if text:
|
| 31 |
+
# Generate text based on the random question
|
| 32 |
+
response = pipe(f"Answer the question: {text}", max_length=150, num_return_sequences=1)
|
| 33 |
+
|
| 34 |
+
# Display the generated response
|
| 35 |
+
st.write(f"Answer: {response[0]['generated_text']}")
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
transformers
|
| 3 |
+
torch
|
| 4 |
+
huggingface_hub
|