Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,6 +10,8 @@ import os
|
|
| 10 |
|
| 11 |
# Read the Hugging Face access token from the environment variable
|
| 12 |
read_token = os.getenv('AccToken')
|
|
|
|
|
|
|
| 13 |
login(read_token)
|
| 14 |
|
| 15 |
# Define a dictionary of conversational models
|
|
@@ -19,7 +21,7 @@ conversational_models = {
|
|
| 19 |
"Perplexity (R1 Post-trained)": "perplexity-ai/r1-1776",
|
| 20 |
"Llama-Instruct by Meta": "meta-llama/Llama-3.2-3B-Instruct",
|
| 21 |
"Mistral": "mistralai/Mistral-7B-v0.1",
|
| 22 |
-
"Gemma": "google/gemma-
|
| 23 |
}
|
| 24 |
|
| 25 |
# Define a dictionary of Text-to-Image models
|
|
@@ -45,17 +47,26 @@ text_to_image_pipelines = {}
|
|
| 45 |
text_to_speech_pipelines = {}
|
| 46 |
|
| 47 |
# Initialize pipelines for other tasks
|
| 48 |
-
visual_qa_pipeline = pipeline("visual-question-answering", model="dandelin/vilt-b32-finetuned-vqa")
|
| 49 |
-
document_qa_pipeline = pipeline("question-answering", model="deepset/roberta-base-squad2")
|
| 50 |
-
image_classification_pipeline = pipeline("image-classification", model="facebook/detr-resnet-50")
|
| 51 |
-
object_detection_pipeline = pipeline("object-detection", model="facebook/detr-resnet-50")
|
| 52 |
-
video_classification_pipeline = pipeline("video-classification", model="facebook/timesformer-base-finetuned-k400")
|
| 53 |
-
summarization_pipeline = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
def load_conversational_model(model_name):
|
| 58 |
if model_name not in conversational_models_loaded:
|
|
|
|
| 59 |
tokenizer = AutoTokenizer.from_pretrained(conversational_models[model_name], use_auth_token=read_token)
|
| 60 |
model = AutoModelForCausalLM.from_pretrained(conversational_models[model_name], use_auth_token=read_token)
|
| 61 |
conversational_tokenizers[model_name] = tokenizer
|
|
@@ -84,6 +95,7 @@ def chat(model_name, user_input, history=[]):
|
|
| 84 |
|
| 85 |
def generate_image(model_name, prompt):
|
| 86 |
if model_name not in text_to_image_pipelines:
|
|
|
|
| 87 |
text_to_image_pipelines[model_name] = StableDiffusionPipeline.from_pretrained(
|
| 88 |
text_to_image_models[model_name], use_auth_token=read_token
|
| 89 |
)
|
|
@@ -93,6 +105,7 @@ def generate_image(model_name, prompt):
|
|
| 93 |
|
| 94 |
def generate_speech(model_name, text):
|
| 95 |
if model_name not in text_to_speech_pipelines:
|
|
|
|
| 96 |
text_to_speech_pipelines[model_name] = pipeline(
|
| 97 |
"text-to-speech", model=text_to_speech_models[model_name], use_auth_token=read_token
|
| 98 |
)
|
|
|
|
| 10 |
|
| 11 |
# Read the Hugging Face access token from the environment variable
|
| 12 |
read_token = os.getenv('AccToken')
|
| 13 |
+
if not read_token:
|
| 14 |
+
raise ValueError("Hugging Face access token not found. Please set the AccToken environment variable.")
|
| 15 |
login(read_token)
|
| 16 |
|
| 17 |
# Define a dictionary of conversational models
|
|
|
|
| 21 |
"Perplexity (R1 Post-trained)": "perplexity-ai/r1-1776",
|
| 22 |
"Llama-Instruct by Meta": "meta-llama/Llama-3.2-3B-Instruct",
|
| 23 |
"Mistral": "mistralai/Mistral-7B-v0.1",
|
| 24 |
+
"Gemma": "google/gemma-2-2b-it",
|
| 25 |
}
|
| 26 |
|
| 27 |
# Define a dictionary of Text-to-Image models
|
|
|
|
| 47 |
text_to_speech_pipelines = {}
|
| 48 |
|
| 49 |
# Initialize pipelines for other tasks
|
| 50 |
+
visual_qa_pipeline = pipeline("visual-question-answering", model="dandelin/vilt-b32-finetuned-vqa", use_auth_token=read_token)
|
| 51 |
+
document_qa_pipeline = pipeline("question-answering", model="deepset/roberta-base-squad2", use_auth_token=read_token)
|
| 52 |
+
image_classification_pipeline = pipeline("image-classification", model="facebook/detr-resnet-50", use_auth_token=read_token)
|
| 53 |
+
object_detection_pipeline = pipeline("object-detection", model="facebook/detr-resnet-50", use_auth_token=read_token)
|
| 54 |
+
video_classification_pipeline = pipeline("video-classification", model="facebook/timesformer-base-finetuned-k400", use_auth_token=read_token)
|
| 55 |
+
summarization_pipeline = pipeline("summarization", model="facebook/bart-large-cnn", use_auth_token=read_token)
|
| 56 |
+
|
| 57 |
+
# Use a different model for text-to-audio if stabilityai/stable-audio-open-1.0 is not supported
|
| 58 |
+
try:
|
| 59 |
+
text_to_audio_pipeline = pipeline("text-to-audio", model="stabilityai/stable-audio-open-1.0", use_auth_token=read_token)
|
| 60 |
+
except ValueError as e:
|
| 61 |
+
print(f"Error loading stabilityai/stable-audio-open-1.0: {e}")
|
| 62 |
+
print("Falling back to a different text-to-audio model.")
|
| 63 |
+
text_to_audio_pipeline = pipeline("text-to-audio", model="microsoft/speecht5_tts", use_auth_token=read_token)
|
| 64 |
+
|
| 65 |
+
audio_classification_pipeline = pipeline("audio-classification", model="facebook/wav2vec2-base", use_auth_token=read_token)
|
| 66 |
|
| 67 |
def load_conversational_model(model_name):
|
| 68 |
if model_name not in conversational_models_loaded:
|
| 69 |
+
print(f"Loading conversational model: {model_name}")
|
| 70 |
tokenizer = AutoTokenizer.from_pretrained(conversational_models[model_name], use_auth_token=read_token)
|
| 71 |
model = AutoModelForCausalLM.from_pretrained(conversational_models[model_name], use_auth_token=read_token)
|
| 72 |
conversational_tokenizers[model_name] = tokenizer
|
|
|
|
| 95 |
|
| 96 |
def generate_image(model_name, prompt):
|
| 97 |
if model_name not in text_to_image_pipelines:
|
| 98 |
+
print(f"Loading text-to-image model: {model_name}")
|
| 99 |
text_to_image_pipelines[model_name] = StableDiffusionPipeline.from_pretrained(
|
| 100 |
text_to_image_models[model_name], use_auth_token=read_token
|
| 101 |
)
|
|
|
|
| 105 |
|
| 106 |
def generate_speech(model_name, text):
|
| 107 |
if model_name not in text_to_speech_pipelines:
|
| 108 |
+
print(f"Loading text-to-speech model: {model_name}")
|
| 109 |
text_to_speech_pipelines[model_name] = pipeline(
|
| 110 |
"text-to-speech", model=text_to_speech_models[model_name], use_auth_token=read_token
|
| 111 |
)
|