Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -11,6 +11,9 @@ import io
|
|
| 11 |
from transformers import pipeline, CLIPProcessor, CLIPModel
|
| 12 |
from datetime import datetime
|
| 13 |
import uvicorn
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
app = FastAPI()
|
| 16 |
|
|
@@ -22,8 +25,19 @@ MAX_CONTENT_LENGTH = 16 * 1024 * 1024 # 16 MB
|
|
| 22 |
if not os.path.exists(UPLOAD_FOLDER):
|
| 23 |
os.makedirs(UPLOAD_FOLDER)
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
# Load the CLIP model for image feature extraction
|
| 29 |
clip_model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
|
|
@@ -31,9 +45,12 @@ clip_processor = CLIPProcessor.from_pretrained("openai/clip-vit-base-patch32")
|
|
| 31 |
|
| 32 |
# Function to generate a more appropriate name based on content
|
| 33 |
def generate_name_based_on_content(text):
|
| 34 |
-
prompt = f"Generate a meaningful file name for the following content: {text[:200]}"
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
|
| 39 |
# Allowed file extensions check
|
|
|
|
| 11 |
from transformers import pipeline, CLIPProcessor, CLIPModel
|
| 12 |
from datetime import datetime
|
| 13 |
import uvicorn
|
| 14 |
+
# Hugging Face GPT or LLM model for content-based name generation
|
| 15 |
+
from langchain.chat_models import ChatOpenAI
|
| 16 |
+
import os
|
| 17 |
|
| 18 |
app = FastAPI()
|
| 19 |
|
|
|
|
| 25 |
if not os.path.exists(UPLOAD_FOLDER):
|
| 26 |
os.makedirs(UPLOAD_FOLDER)
|
| 27 |
|
| 28 |
+
# Load your OpenAI API key from environment variables
|
| 29 |
+
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 30 |
+
|
| 31 |
+
# Ensure the API key is correctly loaded
|
| 32 |
+
if openai_api_key is None:
|
| 33 |
+
raise ValueError("API key not found. Please set your OPENAI_API_KEY environment variable.")
|
| 34 |
+
|
| 35 |
+
# Initialize the LLM (Language Model) with GPT-4o-mini or other available model
|
| 36 |
+
llm = ChatOpenAI(
|
| 37 |
+
model_name="gpt-4o-mini", # Specify the correct model name (e.g., "gpt-4" or "gpt-4o-mini")
|
| 38 |
+
temperature=0, # Set temperature to 0 for deterministic responses (no randomness)
|
| 39 |
+
openai_api_key=openai_api_key # Pass the OpenAI API key
|
| 40 |
+
)
|
| 41 |
|
| 42 |
# Load the CLIP model for image feature extraction
|
| 43 |
clip_model = CLIPModel.from_pretrained("openai/clip-vit-base-patch32")
|
|
|
|
| 45 |
|
| 46 |
# Function to generate a more appropriate name based on content
|
| 47 |
def generate_name_based_on_content(text):
|
| 48 |
+
prompt = f"Generate a meaningful file name for the following content: {text[:200]}" # Truncate text to first 200 characters
|
| 49 |
+
response = llm(prompt) # Get the model's response
|
| 50 |
+
|
| 51 |
+
# Extract the generated file name and clean it
|
| 52 |
+
file_name = response.strip() # Strip any unnecessary whitespace or characters
|
| 53 |
+
return file_name
|
| 54 |
|
| 55 |
|
| 56 |
# Allowed file extensions check
|