Spaces:
Sleeping
Sleeping
Update tools.py
Browse files
tools.py
CHANGED
|
@@ -6,8 +6,17 @@ import pandas as pd
|
|
| 6 |
import whisper
|
| 7 |
import os
|
| 8 |
import tempfile
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
whisper_model = whisper.load_model("base")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
@tool
|
| 13 |
def add(a: int, b: int) -> int:
|
|
@@ -55,6 +64,25 @@ def divide(a: int, b: int) -> int:
|
|
| 55 |
raise ValueError("Cannot divide by zero.")
|
| 56 |
return a / b
|
| 57 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
@tool
|
| 59 |
def read_excel_summary(file_path: str) -> str:
|
| 60 |
"""
|
|
|
|
| 6 |
import whisper
|
| 7 |
import os
|
| 8 |
import tempfile
|
| 9 |
+
from transformers import Blip2Processor, Blip2ForConditionalGeneration
|
| 10 |
+
from PIL import Image
|
| 11 |
+
import torch
|
| 12 |
|
| 13 |
whisper_model = whisper.load_model("base")
|
| 14 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 15 |
+
|
| 16 |
+
# Load model and processor once
|
| 17 |
+
processor = Blip2Processor.from_pretrained("Salesforce/blip2-opt-2.7b")
|
| 18 |
+
model = Blip2ForConditionalGeneration.from_pretrained("Salesforce/blip2-opt-2.7b", device_map="auto").to(device)
|
| 19 |
+
|
| 20 |
|
| 21 |
@tool
|
| 22 |
def add(a: int, b: int) -> int:
|
|
|
|
| 64 |
raise ValueError("Cannot divide by zero.")
|
| 65 |
return a / b
|
| 66 |
|
| 67 |
+
|
| 68 |
+
@tool
|
| 69 |
+
def analyze_image(image_path: str, question: str = "What’s in this image?") -> str:
|
| 70 |
+
"""
|
| 71 |
+
Analyzes an image and answers a question about it using BLIP-2.
|
| 72 |
+
|
| 73 |
+
Args:
|
| 74 |
+
image_path (str): Path to the image file.
|
| 75 |
+
question (str): A natural language question about the image.
|
| 76 |
+
"""
|
| 77 |
+
try:
|
| 78 |
+
image = Image.open(image_path).convert("RGB")
|
| 79 |
+
inputs = processor(images=image, text=question, return_tensors="pt").to(device)
|
| 80 |
+
generated_ids = model.generate(**inputs, max_new_tokens=100)
|
| 81 |
+
response = processor.decode(generated_ids[0], skip_special_tokens=True)
|
| 82 |
+
return response
|
| 83 |
+
except Exception as e:
|
| 84 |
+
return f"Error analyzing image: {str(e)}"
|
| 85 |
+
|
| 86 |
@tool
|
| 87 |
def read_excel_summary(file_path: str) -> str:
|
| 88 |
"""
|