Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,19 +1,5 @@
|
|
1 |
|
2 |
|
3 |
-
import streamlit as st
|
4 |
-
|
5 |
-
|
6 |
-
#from transformers import pipeline
|
7 |
-
|
8 |
-
"""
|
9 |
-
pipe = pipeline ('sentiment-analysis')
|
10 |
-
text = st.text_area("some text")
|
11 |
-
|
12 |
-
if text:
|
13 |
-
out = pipe(text)
|
14 |
-
st.json(out)
|
15 |
-
|
16 |
-
"""
|
17 |
import os
|
18 |
HF_TOKEN = os.getenv('HF_TOKEN')
|
19 |
|
@@ -23,43 +9,49 @@ from huggingface_hub import HfFolder
|
|
23 |
HfFolder.save_token(HF_TOKEN)
|
24 |
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
import torch
|
30 |
-
import torchaudio
|
31 |
-
from einops import rearrange
|
32 |
-
from stable_audio_tools import get_pretrained_model
|
33 |
-
from stable_audio_tools.inference.generation import generate_diffusion_cond
|
34 |
-
|
35 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
36 |
-
|
37 |
-
# Download model
|
38 |
-
model, model_config = get_pretrained_model("stabilityai/stable-audio-open-1.0")
|
39 |
-
sample_rate = model_config["sample_rate"]
|
40 |
-
sample_size = model_config["sample_size"]
|
41 |
-
|
42 |
-
model = model.to(device)
|
43 |
-
|
44 |
-
# Set up text and timing conditioning
|
45 |
-
conditioning = [{
|
46 |
-
"prompt": prompt
|
47 |
-
}]
|
48 |
-
|
49 |
-
# Generate stereo audio
|
50 |
-
output = generate_diffusion_cond(
|
51 |
-
model,
|
52 |
-
conditioning=conditioning,
|
53 |
-
sample_size=sample_size,
|
54 |
-
device=device
|
55 |
-
)
|
56 |
-
|
57 |
-
# Rearrange audio batch to a single sequence
|
58 |
-
output = rearrange(output, "b d n -> d (b n)")
|
59 |
|
60 |
-
#
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
|
|
|
1 |
|
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
import os
|
4 |
HF_TOKEN = os.getenv('HF_TOKEN')
|
5 |
|
|
|
9 |
HfFolder.save_token(HF_TOKEN)
|
10 |
|
11 |
|
12 |
+
from transformers import pipeline, AutoTokenizer, AutoModel
|
13 |
+
import streamlit as st
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
# Set Hugging Face API Token if required
|
16 |
+
"""
|
17 |
+
os.environ["HF_HOME"] = "path_to_your_huggingface_cache_directory"
|
18 |
+
os.environ["TRANSFORMERS_CACHE"] = "path_to_your_transformers_cache_directory"
|
19 |
+
os.environ["HF_DATASETS_CACHE"] = "path_to_your_datasets_cache_directory"
|
20 |
+
os.environ["HF_METRICS_CACHE"] = "path_to_your_metrics_cache_directory"
|
21 |
+
os.environ["HF_MODULES_CACHE"] = "path_to_your_modules_cache_directory"
|
22 |
+
os.environ["HF_TOKEN"] = "your_hugging_face_access_token"
|
23 |
+
"""
|
24 |
|
25 |
+
# Setup Streamlit interface for input
|
26 |
+
st.title("Image to Text Model")
|
27 |
+
|
28 |
+
# Using Pipeline
|
29 |
+
st.header("Using Pipeline for Image Captioning")
|
30 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
31 |
+
|
32 |
+
if uploaded_file is not None:
|
33 |
+
# Assuming the pipeline handles image files directly
|
34 |
+
pipe = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
|
35 |
+
try:
|
36 |
+
result = pipe(uploaded_file.getvalue())
|
37 |
+
st.write("Generated Caption:", result[0]['generated_text'])
|
38 |
+
except Exception as e:
|
39 |
+
st.error(f"Failed to generate caption: {str(e)}")
|
40 |
+
|
41 |
+
# Load model directly for further analysis or different processing steps
|
42 |
+
st.header("Load Model Directly")
|
43 |
+
tokenizer = AutoTokenizer.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
44 |
+
model = AutoModel.from_pretrained("nlpconnect/vit-gpt2-image-captioning")
|
45 |
+
|
46 |
+
# Example of how you might use model and tokenizer directly
|
47 |
+
# This section can be customized based on what you need to do with the model
|
48 |
+
if st.button("Load Model Information"):
|
49 |
+
try:
|
50 |
+
st.text("Model and Tokenizer loaded successfully")
|
51 |
+
# Display some model details, for example:
|
52 |
+
st.text(f"Model Architecture: {model.__class__.__name__}")
|
53 |
+
st.text(f"Tokenizer Type: {tokenizer.__class__.__name__}")
|
54 |
+
except Exception as e:
|
55 |
+
st.error(f"Error loading model: {str(e)}")
|
56 |
|
57 |
|