Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,60 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
-
from PIL import Image
|
| 4 |
import torch
|
|
|
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
try:
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
return None
|
| 12 |
|
| 13 |
# Load models
|
| 14 |
print("Loading models...")
|
| 15 |
image_classifier = load_model_safe("image-classification", "google/vit-base-patch16-224")
|
| 16 |
-
|
| 17 |
sentiment_analyzer = load_model_safe("sentiment-analysis", "distilbert-base-uncased-finetuned-sst-2-english")
|
| 18 |
translator_en_es = load_model_safe("translation", "Helsinki-NLP/opus-mt-en-es")
|
| 19 |
translator_en_fr = load_model_safe("translation", "Helsinki-NLP/opus-mt-en-fr")
|
| 20 |
qa_model = load_model_safe("question-answering", "distilbert-base-cased-distilled-squad")
|
| 21 |
image_caption = load_model_safe("image-to-text", "Salesforce/blip-image-captioning-base")
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
# ==================== FUNCTIONS ====================
|
| 24 |
|
| 25 |
def classify_image(image):
|
| 26 |
-
"""Classify uploaded image"""
|
| 27 |
if image_classifier is None:
|
| 28 |
return {"Error": "Model not loaded"}
|
| 29 |
try:
|
|
@@ -32,18 +63,35 @@ def classify_image(image):
|
|
| 32 |
except Exception as e:
|
| 33 |
return {"Error": str(e)}
|
| 34 |
|
| 35 |
-
def generate_text(prompt, max_length):
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
try:
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
return result[0]['generated_text']
|
| 42 |
except Exception as e:
|
| 43 |
return f"Error: {str(e)}"
|
| 44 |
|
| 45 |
def analyze_sentiment(text):
|
| 46 |
-
"""Analyze sentiment of text"""
|
| 47 |
if sentiment_analyzer is None:
|
| 48 |
return {"Error": "Model not loaded"}
|
| 49 |
try:
|
|
@@ -53,7 +101,6 @@ def analyze_sentiment(text):
|
|
| 53 |
return {"Error": str(e)}
|
| 54 |
|
| 55 |
def translate_text(text, target_lang):
|
| 56 |
-
"""Translate text to target language"""
|
| 57 |
translator = translator_en_es if target_lang == "Spanish" else translator_en_fr
|
| 58 |
if translator is None:
|
| 59 |
return "Model not loaded"
|
|
@@ -64,7 +111,6 @@ def translate_text(text, target_lang):
|
|
| 64 |
return f"Error: {str(e)}"
|
| 65 |
|
| 66 |
def answer_question(context, question):
|
| 67 |
-
"""Answer question based on context"""
|
| 68 |
if qa_model is None:
|
| 69 |
return "Model not loaded"
|
| 70 |
try:
|
|
@@ -74,7 +120,6 @@ def answer_question(context, question):
|
|
| 74 |
return f"Error: {str(e)}"
|
| 75 |
|
| 76 |
def caption_image(image):
|
| 77 |
-
"""Generate caption for image"""
|
| 78 |
if image_caption is None:
|
| 79 |
return "Model not loaded"
|
| 80 |
try:
|
|
@@ -89,8 +134,7 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Multi-Purpose AI Hub") as demo:
|
|
| 89 |
|
| 90 |
gr.Markdown("""
|
| 91 |
# π Multi-Purpose AI Hub
|
| 92 |
-
### Multiple AI Capabilities Using Hugging Face Models
|
| 93 |
-
All models run locally - no API keys needed!
|
| 94 |
""")
|
| 95 |
|
| 96 |
with gr.Tabs():
|
|
@@ -105,19 +149,11 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Multi-Purpose AI Hub") as demo:
|
|
| 105 |
with gr.Column():
|
| 106 |
cls_output = gr.Label(label="Top 5 Predictions", num_top_classes=5)
|
| 107 |
|
| 108 |
-
gr.Examples(
|
| 109 |
-
examples=[
|
| 110 |
-
"https://images.unsplash.com/photo-1514888286974-6c03e2ca1dba?w=400",
|
| 111 |
-
"https://images.unsplash.com/photo-1546527868-ccb7ee7dfa6a?w=400"
|
| 112 |
-
],
|
| 113 |
-
inputs=cls_image
|
| 114 |
-
)
|
| 115 |
-
|
| 116 |
cls_btn.click(classify_image, inputs=cls_image, outputs=cls_output)
|
| 117 |
|
| 118 |
# TAB 2: TEXT GENERATION
|
| 119 |
with gr.Tab("π¬ Text Generation"):
|
| 120 |
-
gr.Markdown("### Generate creative text using GPT-2")
|
| 121 |
with gr.Row():
|
| 122 |
with gr.Column():
|
| 123 |
txt_prompt = gr.Textbox(
|
|
@@ -125,21 +161,22 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Multi-Purpose AI Hub") as demo:
|
|
| 125 |
placeholder="Once upon a time...",
|
| 126 |
lines=4
|
| 127 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
txt_length = gr.Slider(30, 200, value=100, step=10, label="Max Length")
|
| 129 |
txt_btn = gr.Button("β¨ Generate Text", variant="primary", size="lg")
|
| 130 |
with gr.Column():
|
| 131 |
txt_output = gr.Textbox(label="Generated Text", lines=10)
|
| 132 |
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
["In a world where technology", 120]
|
| 138 |
-
],
|
| 139 |
-
inputs=[txt_prompt, txt_length]
|
| 140 |
-
)
|
| 141 |
|
| 142 |
-
txt_btn.click(generate_text, inputs=[txt_prompt, txt_length], outputs=txt_output)
|
| 143 |
|
| 144 |
# TAB 3: SENTIMENT ANALYSIS
|
| 145 |
with gr.Tab("π Sentiment Analysis"):
|
|
@@ -148,22 +185,13 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Multi-Purpose AI Hub") as demo:
|
|
| 148 |
with gr.Column():
|
| 149 |
sent_text = gr.Textbox(
|
| 150 |
label="Text to Analyze",
|
| 151 |
-
placeholder="I love this product!
|
| 152 |
lines=5
|
| 153 |
)
|
| 154 |
sent_btn = gr.Button("π Analyze Sentiment", variant="primary", size="lg")
|
| 155 |
with gr.Column():
|
| 156 |
sent_output = gr.Label(label="Sentiment Score")
|
| 157 |
|
| 158 |
-
gr.Examples(
|
| 159 |
-
examples=[
|
| 160 |
-
"I absolutely love this! Best purchase ever!",
|
| 161 |
-
"This is terrible and disappointing.",
|
| 162 |
-
"It's okay, nothing special but not bad either."
|
| 163 |
-
],
|
| 164 |
-
inputs=sent_text
|
| 165 |
-
)
|
| 166 |
-
|
| 167 |
sent_btn.click(analyze_sentiment, inputs=sent_text, outputs=sent_output)
|
| 168 |
|
| 169 |
# TAB 4: TRANSLATION
|
|
@@ -185,15 +213,6 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Multi-Purpose AI Hub") as demo:
|
|
| 185 |
with gr.Column():
|
| 186 |
trans_output = gr.Textbox(label="Translation", lines=6)
|
| 187 |
|
| 188 |
-
gr.Examples(
|
| 189 |
-
examples=[
|
| 190 |
-
["Hello, how are you today?", "Spanish"],
|
| 191 |
-
["I love learning new languages.", "French"],
|
| 192 |
-
["Thank you for your help!", "Spanish"]
|
| 193 |
-
],
|
| 194 |
-
inputs=[trans_text, trans_lang]
|
| 195 |
-
)
|
| 196 |
-
|
| 197 |
trans_btn.click(translate_text, inputs=[trans_text, trans_lang], outputs=trans_output)
|
| 198 |
|
| 199 |
# TAB 5: QUESTION ANSWERING
|
|
@@ -214,20 +233,6 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Multi-Purpose AI Hub") as demo:
|
|
| 214 |
with gr.Column():
|
| 215 |
qa_output = gr.Textbox(label="Answer", lines=4)
|
| 216 |
|
| 217 |
-
gr.Examples(
|
| 218 |
-
examples=[
|
| 219 |
-
[
|
| 220 |
-
"The Eiffel Tower is located in Paris, France. It was built in 1889 and stands 330 meters tall.",
|
| 221 |
-
"Where is the Eiffel Tower located?"
|
| 222 |
-
],
|
| 223 |
-
[
|
| 224 |
-
"Python is a high-level programming language created by Guido van Rossum in 1991. It emphasizes code readability.",
|
| 225 |
-
"Who created Python?"
|
| 226 |
-
]
|
| 227 |
-
],
|
| 228 |
-
inputs=[qa_context, qa_question]
|
| 229 |
-
)
|
| 230 |
-
|
| 231 |
qa_btn.click(answer_question, inputs=[qa_context, qa_question], outputs=qa_output)
|
| 232 |
|
| 233 |
# TAB 6: IMAGE CAPTIONING
|
|
@@ -240,27 +245,20 @@ with gr.Blocks(theme=gr.themes.Soft(), title="Multi-Purpose AI Hub") as demo:
|
|
| 240 |
with gr.Column():
|
| 241 |
cap_output = gr.Textbox(label="Generated Caption", lines=4)
|
| 242 |
|
| 243 |
-
gr.Examples(
|
| 244 |
-
examples=[
|
| 245 |
-
"https://images.unsplash.com/photo-1506905925346-21bda4d32df4?w=400"
|
| 246 |
-
],
|
| 247 |
-
inputs=cap_image
|
| 248 |
-
)
|
| 249 |
-
|
| 250 |
cap_btn.click(caption_image, inputs=cap_image, outputs=cap_output)
|
| 251 |
|
| 252 |
gr.Markdown("""
|
| 253 |
---
|
| 254 |
### π Models Used:
|
| 255 |
- **Image Classification**: Google Vision Transformer (ViT)
|
| 256 |
-
- **Text Generation**: GPT-2
|
| 257 |
- **Sentiment Analysis**: DistilBERT (SST-2)
|
| 258 |
- **Translation**: Helsinki-NLP OPUS-MT
|
| 259 |
- **Question Answering**: DistilBERT (SQuAD)
|
| 260 |
- **Image Captioning**: Salesforce BLIP
|
| 261 |
|
| 262 |
-
π **All models
|
| 263 |
-
β‘ **
|
| 264 |
""")
|
| 265 |
|
| 266 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
|
|
|
| 3 |
import torch
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
+
# Get token from environment
|
| 7 |
+
token = os.getenv("HF_TOKEN")
|
| 8 |
+
|
| 9 |
+
# Initialize models
|
| 10 |
+
print("Initializing models...")
|
| 11 |
+
image_classifier = None
|
| 12 |
+
text_generator_gpt2 = None
|
| 13 |
+
text_generator_llama = None
|
| 14 |
+
sentiment_analyzer = None
|
| 15 |
+
translator_en_es = None
|
| 16 |
+
translator_en_fr = None
|
| 17 |
+
qa_model = None
|
| 18 |
+
image_caption = None
|
| 19 |
+
|
| 20 |
+
def load_model_safe(task, model_name, use_token=False):
|
| 21 |
try:
|
| 22 |
+
if use_token:
|
| 23 |
+
return pipeline(task, model=model_name, token=token)
|
| 24 |
+
else:
|
| 25 |
+
return pipeline(task, model=model_name)
|
| 26 |
+
except Exception as e:
|
| 27 |
+
print(f"Error loading {model_name}: {e}")
|
| 28 |
return None
|
| 29 |
|
| 30 |
# Load models
|
| 31 |
print("Loading models...")
|
| 32 |
image_classifier = load_model_safe("image-classification", "google/vit-base-patch16-224")
|
| 33 |
+
text_generator_gpt2 = load_model_safe("text-generation", "gpt2")
|
| 34 |
sentiment_analyzer = load_model_safe("sentiment-analysis", "distilbert-base-uncased-finetuned-sst-2-english")
|
| 35 |
translator_en_es = load_model_safe("translation", "Helsinki-NLP/opus-mt-en-es")
|
| 36 |
translator_en_fr = load_model_safe("translation", "Helsinki-NLP/opus-mt-en-fr")
|
| 37 |
qa_model = load_model_safe("question-answering", "distilbert-base-cased-distilled-squad")
|
| 38 |
image_caption = load_model_safe("image-to-text", "Salesforce/blip-image-captioning-base")
|
| 39 |
|
| 40 |
+
# Load Llama with token
|
| 41 |
+
try:
|
| 42 |
+
print("Loading Llama 3.2...")
|
| 43 |
+
text_generator_llama = pipeline(
|
| 44 |
+
"text-generation",
|
| 45 |
+
model="meta-llama/Llama-3.2-1B-Instruct",
|
| 46 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 47 |
+
device_map="auto" if torch.cuda.is_available() else None,
|
| 48 |
+
token=token
|
| 49 |
+
)
|
| 50 |
+
print("Llama loaded successfully!")
|
| 51 |
+
except Exception as e:
|
| 52 |
+
print(f"Error loading Llama: {e}")
|
| 53 |
+
text_generator_llama = None
|
| 54 |
+
|
| 55 |
# ==================== FUNCTIONS ====================
|
| 56 |
|
| 57 |
def classify_image(image):
|
|
|
|
| 58 |
if image_classifier is None:
|
| 59 |
return {"Error": "Model not loaded"}
|
| 60 |
try:
|
|
|
|
| 63 |
except Exception as e:
|
| 64 |
return {"Error": str(e)}
|
| 65 |
|
| 66 |
+
def generate_text(prompt, max_length, model_choice):
|
| 67 |
+
if model_choice == "GPT-2":
|
| 68 |
+
model = text_generator_gpt2
|
| 69 |
+
elif model_choice == "Llama 3.2 1B":
|
| 70 |
+
model = text_generator_llama
|
| 71 |
+
else:
|
| 72 |
+
return "Model not selected"
|
| 73 |
+
|
| 74 |
+
if model is None:
|
| 75 |
+
return "Model not loaded. Make sure you've accepted the Llama license on Hugging Face."
|
| 76 |
+
|
| 77 |
try:
|
| 78 |
+
if model_choice == "Llama 3.2 1B":
|
| 79 |
+
result = model(
|
| 80 |
+
prompt,
|
| 81 |
+
max_new_tokens=int(max_length),
|
| 82 |
+
temperature=0.7,
|
| 83 |
+
top_p=0.9,
|
| 84 |
+
do_sample=True,
|
| 85 |
+
pad_token_id=model.tokenizer.eos_token_id
|
| 86 |
+
)
|
| 87 |
+
else:
|
| 88 |
+
result = model(prompt, max_length=int(max_length), num_return_sequences=1)
|
| 89 |
+
|
| 90 |
return result[0]['generated_text']
|
| 91 |
except Exception as e:
|
| 92 |
return f"Error: {str(e)}"
|
| 93 |
|
| 94 |
def analyze_sentiment(text):
|
|
|
|
| 95 |
if sentiment_analyzer is None:
|
| 96 |
return {"Error": "Model not loaded"}
|
| 97 |
try:
|
|
|
|
| 101 |
return {"Error": str(e)}
|
| 102 |
|
| 103 |
def translate_text(text, target_lang):
|
|
|
|
| 104 |
translator = translator_en_es if target_lang == "Spanish" else translator_en_fr
|
| 105 |
if translator is None:
|
| 106 |
return "Model not loaded"
|
|
|
|
| 111 |
return f"Error: {str(e)}"
|
| 112 |
|
| 113 |
def answer_question(context, question):
|
|
|
|
| 114 |
if qa_model is None:
|
| 115 |
return "Model not loaded"
|
| 116 |
try:
|
|
|
|
| 120 |
return f"Error: {str(e)}"
|
| 121 |
|
| 122 |
def caption_image(image):
|
|
|
|
| 123 |
if image_caption is None:
|
| 124 |
return "Model not loaded"
|
| 125 |
try:
|
|
|
|
| 134 |
|
| 135 |
gr.Markdown("""
|
| 136 |
# π Multi-Purpose AI Hub
|
| 137 |
+
### Multiple AI Capabilities Using Hugging Face Models - Now with Llama 3.2!
|
|
|
|
| 138 |
""")
|
| 139 |
|
| 140 |
with gr.Tabs():
|
|
|
|
| 149 |
with gr.Column():
|
| 150 |
cls_output = gr.Label(label="Top 5 Predictions", num_top_classes=5)
|
| 151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
cls_btn.click(classify_image, inputs=cls_image, outputs=cls_output)
|
| 153 |
|
| 154 |
# TAB 2: TEXT GENERATION
|
| 155 |
with gr.Tab("π¬ Text Generation"):
|
| 156 |
+
gr.Markdown("### Generate creative text using GPT-2 or Llama 3.2")
|
| 157 |
with gr.Row():
|
| 158 |
with gr.Column():
|
| 159 |
txt_prompt = gr.Textbox(
|
|
|
|
| 161 |
placeholder="Once upon a time...",
|
| 162 |
lines=4
|
| 163 |
)
|
| 164 |
+
txt_model_choice = gr.Radio(
|
| 165 |
+
choices=["GPT-2", "Llama 3.2 1B"],
|
| 166 |
+
value="Llama 3.2 1B" if text_generator_llama else "GPT-2",
|
| 167 |
+
label="Model"
|
| 168 |
+
)
|
| 169 |
txt_length = gr.Slider(30, 200, value=100, step=10, label="Max Length")
|
| 170 |
txt_btn = gr.Button("β¨ Generate Text", variant="primary", size="lg")
|
| 171 |
with gr.Column():
|
| 172 |
txt_output = gr.Textbox(label="Generated Text", lines=10)
|
| 173 |
|
| 174 |
+
if text_generator_llama:
|
| 175 |
+
gr.Markdown("β
**Llama 3.2 is loaded and ready to use!**")
|
| 176 |
+
else:
|
| 177 |
+
gr.Markdown("β οΈ **Llama not available. Make sure you accepted the license and added HF_TOKEN.**")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
|
| 179 |
+
txt_btn.click(generate_text, inputs=[txt_prompt, txt_length, txt_model_choice], outputs=txt_output)
|
| 180 |
|
| 181 |
# TAB 3: SENTIMENT ANALYSIS
|
| 182 |
with gr.Tab("π Sentiment Analysis"):
|
|
|
|
| 185 |
with gr.Column():
|
| 186 |
sent_text = gr.Textbox(
|
| 187 |
label="Text to Analyze",
|
| 188 |
+
placeholder="I love this product!",
|
| 189 |
lines=5
|
| 190 |
)
|
| 191 |
sent_btn = gr.Button("π Analyze Sentiment", variant="primary", size="lg")
|
| 192 |
with gr.Column():
|
| 193 |
sent_output = gr.Label(label="Sentiment Score")
|
| 194 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
sent_btn.click(analyze_sentiment, inputs=sent_text, outputs=sent_output)
|
| 196 |
|
| 197 |
# TAB 4: TRANSLATION
|
|
|
|
| 213 |
with gr.Column():
|
| 214 |
trans_output = gr.Textbox(label="Translation", lines=6)
|
| 215 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 216 |
trans_btn.click(translate_text, inputs=[trans_text, trans_lang], outputs=trans_output)
|
| 217 |
|
| 218 |
# TAB 5: QUESTION ANSWERING
|
|
|
|
| 233 |
with gr.Column():
|
| 234 |
qa_output = gr.Textbox(label="Answer", lines=4)
|
| 235 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
qa_btn.click(answer_question, inputs=[qa_context, qa_question], outputs=qa_output)
|
| 237 |
|
| 238 |
# TAB 6: IMAGE CAPTIONING
|
|
|
|
| 245 |
with gr.Column():
|
| 246 |
cap_output = gr.Textbox(label="Generated Caption", lines=4)
|
| 247 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
cap_btn.click(caption_image, inputs=cap_image, outputs=cap_output)
|
| 249 |
|
| 250 |
gr.Markdown("""
|
| 251 |
---
|
| 252 |
### π Models Used:
|
| 253 |
- **Image Classification**: Google Vision Transformer (ViT)
|
| 254 |
+
- **Text Generation**: GPT-2 & **Llama 3.2 1B** π¦
|
| 255 |
- **Sentiment Analysis**: DistilBERT (SST-2)
|
| 256 |
- **Translation**: Helsinki-NLP OPUS-MT
|
| 257 |
- **Question Answering**: DistilBERT (SQuAD)
|
| 258 |
- **Image Captioning**: Salesforce BLIP
|
| 259 |
|
| 260 |
+
π **All models authenticated with your HF token**
|
| 261 |
+
β‘ **Llama 3.2 now available for advanced text generation!**
|
| 262 |
""")
|
| 263 |
|
| 264 |
if __name__ == "__main__":
|