Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,54 +26,111 @@ CONTEXT_SIZES = {
|
|
| 26 |
}
|
| 27 |
|
| 28 |
MODEL_CONTEXT_SIZES = {
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
}
|
| 50 |
|
| 51 |
class ModelRegistry:
|
| 52 |
def __init__(self):
|
| 53 |
# HuggingFace Models
|
| 54 |
self.hf_models = {
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
# Default Groq Models
|
| 69 |
-
self.default_groq_models = {
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
self.groq_models = self._fetch_groq_models()
|
| 79 |
|
|
@@ -341,12 +398,15 @@ def send_to_groq(prompt: str, model_name: str, api_key: str) -> str:
|
|
| 341 |
return f"Error with Groq API: {str(e)}"
|
| 342 |
|
| 343 |
def send_to_openai(prompt: str, api_key: str, model: str = "gpt-3.5-turbo") -> str:
|
| 344 |
-
"""Send prompt to OpenAI API
|
| 345 |
try:
|
| 346 |
-
import
|
| 347 |
-
openai.api_key = api_key
|
| 348 |
|
| 349 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 350 |
model=model,
|
| 351 |
messages=[
|
| 352 |
{"role": "system", "content": "You are a helpful assistant that provides detailed responses."},
|
|
@@ -356,7 +416,15 @@ def send_to_openai(prompt: str, api_key: str, model: str = "gpt-3.5-turbo") -> s
|
|
| 356 |
max_tokens=500,
|
| 357 |
top_p=0.95
|
| 358 |
)
|
| 359 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 360 |
except Exception as e:
|
| 361 |
logging.error(f"OpenAI API error: {e}")
|
| 362 |
return f"Error with OpenAI API: {str(e)}"
|
|
|
|
| 26 |
}
|
| 27 |
|
| 28 |
MODEL_CONTEXT_SIZES = {
|
| 29 |
+
"Clipboard only": 4096,
|
| 30 |
+
"OpenAI ChatGPT": {
|
| 31 |
+
"gpt-3.5-turbo": 16385,
|
| 32 |
+
"gpt-3.5-turbo-0125": 16385,
|
| 33 |
+
"gpt-3.5-turbo-1106": 16385,
|
| 34 |
+
"gpt-3.5-turbo-instruct": 4096,
|
| 35 |
+
"gpt-4": 8192,
|
| 36 |
+
"gpt-4-0314": 8192,
|
| 37 |
+
"gpt-4-0613": 8192,
|
| 38 |
+
"gpt-4-turbo": 128000,
|
| 39 |
+
"gpt-4-turbo-2024-04-09": 128000,
|
| 40 |
+
"gpt-4-turbo-preview": 128000,
|
| 41 |
+
"gpt-4-0125-preview": 128000,
|
| 42 |
+
"gpt-4-1106-preview": 128000,
|
| 43 |
+
"gpt-4o": 128000,
|
| 44 |
+
"gpt-4o-2024-11-20": 128000,
|
| 45 |
+
"gpt-4o-2024-08-06": 128000,
|
| 46 |
+
"gpt-4o-2024-05-13": 128000,
|
| 47 |
+
"chatgpt-4o-latest": 128000,
|
| 48 |
+
"gpt-4o-mini": 128000,
|
| 49 |
+
"gpt-4o-mini-2024-07-18": 128000,
|
| 50 |
+
"gpt-4o-realtime-preview": 128000,
|
| 51 |
+
"gpt-4o-realtime-preview-2024-10-01": 128000,
|
| 52 |
+
"gpt-4o-audio-preview": 128000,
|
| 53 |
+
"gpt-4o-audio-preview-2024-10-01": 128000,
|
| 54 |
+
"o1-preview": 128000,
|
| 55 |
+
"o1-preview-2024-09-12": 128000,
|
| 56 |
+
"o1-mini": 128000,
|
| 57 |
+
"o1-mini-2024-09-12": 128000,
|
| 58 |
+
},
|
| 59 |
+
"HuggingFace Inference": {
|
| 60 |
+
"microsoft/phi-3-mini-4k-instruct": 4096,
|
| 61 |
+
"microsoft/Phi-3-mini-128k-instruct": 131072, # Added Phi-3 128k
|
| 62 |
+
"HuggingFaceH4/zephyr-7b-beta": 8192,
|
| 63 |
+
"deepseek-ai/DeepSeek-Coder-V2-Instruct": 8192,
|
| 64 |
+
"meta-llama/Llama-3-8b-Instruct": 8192,
|
| 65 |
+
"mistralai/Mistral-7B-Instruct-v0.3": 32768,
|
| 66 |
+
"NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO": 32768,
|
| 67 |
+
"microsoft/Phi-3.5-mini-instruct": 4096,
|
| 68 |
+
"HuggingFaceTB/SmolLM2-1.7B-Instruct": 2048,
|
| 69 |
+
"google/gemma-2-2b-it": 2048,
|
| 70 |
+
"openai-community/gpt2": 1024,
|
| 71 |
+
"microsoft/phi-2": 2048,
|
| 72 |
+
"TinyLlama/TinyLlama-1.1B-Chat-v1.0": 2048
|
| 73 |
+
},
|
| 74 |
+
"Groq API": {
|
| 75 |
+
"gemma2-9b-it": 8192,
|
| 76 |
+
"gemma-7b-it": 8192,
|
| 77 |
+
"llama-3.3-70b-versatile": 131072,
|
| 78 |
+
"llama-3.1-70b-versatile": 131072, # Deprecated
|
| 79 |
+
"llama-3.1-8b-instant": 131072,
|
| 80 |
+
"llama-guard-3-8b": 8192,
|
| 81 |
+
"llama3-70b-8192": 8192,
|
| 82 |
+
"llama3-8b-8192": 8192,
|
| 83 |
+
"mixtral-8x7b-32768": 32768,
|
| 84 |
+
"llama3-groq-70b-8192-tool-use-preview": 8192,
|
| 85 |
+
"llama3-groq-8b-8192-tool-use-preview": 8192,
|
| 86 |
+
"llama-3.3-70b-specdec": 131072,
|
| 87 |
+
"llama-3.1-70b-specdec": 131072,
|
| 88 |
+
"llama-3.2-1b-preview": 131072,
|
| 89 |
+
"llama-3.2-3b-preview": 131072,
|
| 90 |
+
}
|
| 91 |
}
|
| 92 |
|
| 93 |
class ModelRegistry:
|
| 94 |
def __init__(self):
|
| 95 |
# HuggingFace Models
|
| 96 |
self.hf_models = {
|
| 97 |
+
"Phi-3 Mini 4K": "microsoft/phi-3-mini-4k-instruct",
|
| 98 |
+
"Phi-3 Mini 128k": "microsoft/Phi-3-mini-128k-instruct", # Added
|
| 99 |
+
"Zephyr 7B Beta": "HuggingFaceH4/zephyr-7b-beta",
|
| 100 |
+
"DeepSeek Coder V2": "deepseek-ai/DeepSeek-Coder-V2-Instruct",
|
| 101 |
+
"Meta Llama 3.1 8B": "meta-llama/Llama-3-8b-Instruct",
|
| 102 |
+
"Meta Llama 3.1 70B": "meta-llama/Meta-Llama-3.1-70B-Instruct",
|
| 103 |
+
"Mixtral 7B": "mistralai/Mistral-7B-Instruct-v0.3",
|
| 104 |
+
"Nous-Hermes": "NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO",
|
| 105 |
+
"Cohere Command R+": "CohereForAI/c4ai-command-r-plus",
|
| 106 |
+
"Aya 23-35B": "CohereForAI/aya-23-35B",
|
| 107 |
+
"Phi-3.5 Mini": "microsoft/Phi-3.5-mini-instruct", # Added
|
| 108 |
+
"SmolLM2 1.7B": "HuggingFaceTB/SmolLM2-1.7B-Instruct", # Added
|
| 109 |
+
"Gemma 2 2B": "google/gemma-2-2b-it", # Added
|
| 110 |
+
"GPT2": "openai-community/gpt2", # Added
|
| 111 |
+
"Phi-2": "microsoft/phi-2", # Added
|
| 112 |
+
"TinyLlama 1.1B": "TinyLlama/TinyLlama-1.1B-Chat-v1.0", # Added
|
| 113 |
+
"Custom Model": "" # Keep for custom models
|
| 114 |
+
}
|
| 115 |
|
| 116 |
# Default Groq Models
|
| 117 |
+
self.default_groq_models = { # Keep defaults in case fetching fails
|
| 118 |
+
"gemma2-9b-it": "gemma2-9b-it",
|
| 119 |
+
"gemma-7b-it": "gemma-7b-it",
|
| 120 |
+
"llama-3.3-70b-versatile": "llama-3.3-70b-versatile",
|
| 121 |
+
"llama-3.1-70b-versatile": "llama-3.1-70b-versatile", # Deprecated
|
| 122 |
+
"llama-3.1-8b-instant": "llama-3.1-8b-instant",
|
| 123 |
+
"llama-guard-3-8b": "llama-guard-3-8b",
|
| 124 |
+
"llama3-70b-8192": "llama3-70b-8192",
|
| 125 |
+
"llama3-8b-8192": "llama3-8b-8192",
|
| 126 |
+
"mixtral-8x7b-32768": "mixtral-8x7b-32768",
|
| 127 |
+
"llama3-groq-70b-8192-tool-use-preview": "llama3-groq-70b-8192-tool-use-preview",
|
| 128 |
+
"llama3-groq-8b-8192-tool-use-preview": "llama3-groq-8b-8192-tool-use-preview",
|
| 129 |
+
"llama-3.3-70b-specdec": "llama-3.3-70b-specdec",
|
| 130 |
+
"llama-3.1-70b-specdec": "llama-3.1-70b-specdec",
|
| 131 |
+
"llama-3.2-1b-preview": "llama-3.2-1b-preview",
|
| 132 |
+
"llama-3.2-3b-preview": "llama-3.2-3b-preview",
|
| 133 |
+
}
|
| 134 |
|
| 135 |
self.groq_models = self._fetch_groq_models()
|
| 136 |
|
|
|
|
| 398 |
return f"Error with Groq API: {str(e)}"
|
| 399 |
|
| 400 |
def send_to_openai(prompt: str, api_key: str, model: str = "gpt-3.5-turbo") -> str:
|
| 401 |
+
"""Send prompt to OpenAI API using the new v1.0+ client format."""
|
| 402 |
try:
|
| 403 |
+
from openai import OpenAI # Import the new client
|
|
|
|
| 404 |
|
| 405 |
+
# Initialize the client with the API key
|
| 406 |
+
client = OpenAI(api_key=api_key)
|
| 407 |
+
|
| 408 |
+
# Create chat completion using new format
|
| 409 |
+
response = client.chat.completions.create(
|
| 410 |
model=model,
|
| 411 |
messages=[
|
| 412 |
{"role": "system", "content": "You are a helpful assistant that provides detailed responses."},
|
|
|
|
| 416 |
max_tokens=500,
|
| 417 |
top_p=0.95
|
| 418 |
)
|
| 419 |
+
|
| 420 |
+
# Extract the response content
|
| 421 |
+
if response.choices and len(response.choices) > 0:
|
| 422 |
+
return response.choices[0].message.content
|
| 423 |
+
else:
|
| 424 |
+
return "Error: No response generated"
|
| 425 |
+
|
| 426 |
+
except ImportError:
|
| 427 |
+
return "Error: Please install the latest version of openai package (pip install --upgrade openai)"
|
| 428 |
except Exception as e:
|
| 429 |
logging.error(f"OpenAI API error: {e}")
|
| 430 |
return f"Error with OpenAI API: {str(e)}"
|