Spaces:
Sleeping
Sleeping
Update runner.py
Browse files
runner.py
CHANGED
|
@@ -1,6 +1,11 @@
|
|
| 1 |
import os
|
| 2 |
from openai import OpenAI
|
| 3 |
from dotenv import load_dotenv
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
# Load environment variables
|
| 6 |
load_dotenv()
|
|
@@ -53,10 +58,110 @@ def run_custom_model(model_name, question):
|
|
| 53 |
# You'll need to implement this based on how your custom models work
|
| 54 |
return f"Custom model {model_name} response: This is a placeholder answer for the question provided."
|
| 55 |
|
| 56 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
if model_name == "gpt-4o-mini":
|
| 58 |
-
return run_gpt4o_mini(
|
| 59 |
elif model_name == "gpt-4o":
|
| 60 |
-
return run_gpt4o(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
else:
|
| 62 |
-
return run_custom_model(model_name,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
from openai import OpenAI
|
| 3 |
from dotenv import load_dotenv
|
| 4 |
+
import openai
|
| 5 |
+
import io
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import base64 # {{ edit_add: Import base64 for image conversion }}
|
| 8 |
+
import requests # Add this import for making HTTP requests to Hugging Face
|
| 9 |
|
| 10 |
# Load environment variables
|
| 11 |
load_dotenv()
|
|
|
|
| 58 |
# You'll need to implement this based on how your custom models work
|
| 59 |
return f"Custom model {model_name} response: This is a placeholder answer for the question provided."
|
| 60 |
|
| 61 |
+
def run_huggingface_model(endpoint, token, prompt, context):
|
| 62 |
+
"""
|
| 63 |
+
Runs the Hugging Face model with the provided prompt and context.
|
| 64 |
+
|
| 65 |
+
Args:
|
| 66 |
+
endpoint (str): The Hugging Face model endpoint URL.
|
| 67 |
+
token (str): The Hugging Face API token.
|
| 68 |
+
prompt (str): The user's prompt.
|
| 69 |
+
context (str): The context related to the prompt.
|
| 70 |
+
|
| 71 |
+
Returns:
|
| 72 |
+
str: The generated response from the Hugging Face model.
|
| 73 |
+
"""
|
| 74 |
+
import os
|
| 75 |
+
import requests
|
| 76 |
+
import json
|
| 77 |
+
|
| 78 |
+
headers = {"Authorization": f"Bearer {token}"}
|
| 79 |
+
combined_input = f"{context}\n\n{prompt}" if context else prompt
|
| 80 |
+
payload = {"inputs": combined_input}
|
| 81 |
+
|
| 82 |
+
try:
|
| 83 |
+
response = requests.post(endpoint, headers=headers, json=payload)
|
| 84 |
+
response.raise_for_status()
|
| 85 |
+
generated_text = response.json()[0]['generated_text']
|
| 86 |
+
return generated_text
|
| 87 |
+
except requests.exceptions.RequestException as e:
|
| 88 |
+
print(f"Error calling Hugging Face API: {e}")
|
| 89 |
+
return None
|
| 90 |
+
|
| 91 |
+
def run_model(model_name, prompt, context=""):
|
| 92 |
+
"""
|
| 93 |
+
Runs the specified model with the given prompt and context.
|
| 94 |
+
|
| 95 |
+
Args:
|
| 96 |
+
model_name (str): The name of the model to run.
|
| 97 |
+
prompt (str): The user's prompt.
|
| 98 |
+
context (str, optional): The context related to the prompt. Defaults to "".
|
| 99 |
+
|
| 100 |
+
Returns:
|
| 101 |
+
str: The generated response from the model.
|
| 102 |
+
"""
|
| 103 |
+
from pymongo import MongoClient
|
| 104 |
+
from dotenv import load_dotenv
|
| 105 |
+
import os
|
| 106 |
+
|
| 107 |
+
# Load environment variables
|
| 108 |
+
load_dotenv()
|
| 109 |
+
|
| 110 |
+
# MongoDB connection
|
| 111 |
+
mongodb_uri = os.getenv('MONGODB_URI')
|
| 112 |
+
mongo_client = MongoClient(mongodb_uri)
|
| 113 |
+
db = mongo_client['llm_evaluation_system']
|
| 114 |
+
users_collection = db['users']
|
| 115 |
+
|
| 116 |
if model_name == "gpt-4o-mini":
|
| 117 |
+
return run_gpt4o_mini(prompt)
|
| 118 |
elif model_name == "gpt-4o":
|
| 119 |
+
return run_gpt4o(prompt)
|
| 120 |
+
elif model_name.startswith("HF_"):
|
| 121 |
+
# Fetch model details from the database
|
| 122 |
+
user = users_collection.find_one({"models.model_name": model_name})
|
| 123 |
+
if user:
|
| 124 |
+
model = next((m for m in user['models'] if m['model_name'] == model_name), None)
|
| 125 |
+
if model:
|
| 126 |
+
return run_huggingface_model(model['model_link'], model['model_api_token'], prompt, context)
|
| 127 |
+
print(f"Hugging Face model {model_name} not found")
|
| 128 |
+
return None
|
| 129 |
else:
|
| 130 |
+
return run_custom_model(model_name, prompt)
|
| 131 |
+
|
| 132 |
+
# {{ edit_final: Add function to summarize images }}
|
| 133 |
+
def summarize_image(image_bytes: bytes) -> str:
|
| 134 |
+
try:
|
| 135 |
+
# Convert bytes to base64
|
| 136 |
+
base64_image = base64.b64encode(image_bytes).decode('utf-8')
|
| 137 |
+
|
| 138 |
+
payload = {
|
| 139 |
+
"model": "gpt-4o-mini",
|
| 140 |
+
"messages": [
|
| 141 |
+
{
|
| 142 |
+
"role": "user",
|
| 143 |
+
"content": [
|
| 144 |
+
{
|
| 145 |
+
"type": "text",
|
| 146 |
+
"text": "Please describe and summarize this image."
|
| 147 |
+
},
|
| 148 |
+
{
|
| 149 |
+
"type": "image_url",
|
| 150 |
+
"image_url": {
|
| 151 |
+
"url": f"data:image/jpeg;base64,{base64_image}"
|
| 152 |
+
}
|
| 153 |
+
}
|
| 154 |
+
]
|
| 155 |
+
}
|
| 156 |
+
],
|
| 157 |
+
"max_tokens": 300
|
| 158 |
+
}
|
| 159 |
+
|
| 160 |
+
response = openai_client.chat.completions.create(**payload)
|
| 161 |
+
|
| 162 |
+
summary = response.choices[0].message.content.strip()
|
| 163 |
+
return summary
|
| 164 |
+
|
| 165 |
+
except Exception as e:
|
| 166 |
+
print(f"Error in summarize_image: {e}")
|
| 167 |
+
return "Failed to summarize the image."
|