Spaces:
Paused
Paused
Update app.py
Browse files25 unmodified lines262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879261 unmodified lines3413423433443453463473483493503513523 unmodified lines35635735835936036136225 unmodified lines}MODEL_CONTEXT_SIZES = {"Clipboard only": 4096,"OpenAI ChatGPT": {"gpt-3.5-turbo": 4096,"gpt-4": 8192,"gpt-4-32k": 32768},"HuggingFace Inference": {"microsoft/phi-3-mini-4k-instruct": 4096,"HuggingFaceH4/zephyr-7b-beta": 8192,"deepseek-ai/DeepSeek-Coder-V2-Instruct": 8192,"meta-llama/Llama-3-8b-Instruct": 8192,"mistralai/Mistral-7B-Instruct-v0.3": 32768,"NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO": 32768},"Groq API": {"gemma-7b-it": 8192,"llama-3.1-70b": 32768,"mixtral-8x7b-32768": 32768,"llama-3.1-8b": 8192}}class ModelRegistry:def __init__(self):# HuggingFace Modelsself.hf_models = {"Phi-3 Mini 4K": "microsoft/phi-3-mini-4k-instruct","Phi-3 Mini 128k": "microsoft/Phi-3-mini-128k-instruct","Zephyr 7B Beta": "HuggingFaceH4/zephyr-7b-beta","DeepSeek Coder V2": "deepseek-ai/DeepSeek-Coder-V2-Instruct","Meta Llama 3.1 8B": "meta-llama/Llama-3-8b-Instruct","Meta Llama 3.1 70B": "meta-llama/Meta-Llama-3.1-70B-Instruct","Mixtral 7B": "mistralai/Mistral-7B-Instruct-v0.3","Nous-Hermes": "NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO","Cohere Command R+": "CohereForAI/c4ai-command-r-plus","Aya 23-35B": "CohereForAI/aya-23-35B","Custom Model": ""}# Default Groq Modelsself.default_groq_models = {"gemma-7b-it": "gemma-7b-it","llama-3.1-70b-8192": "llama-3.1-70b-8192","llama-3.1-70b-versatile": "llama-3.1-70b-versatile","mixtral-8x7b-32768": "mixtral-8x7b-32768","llama-3.1-8b-instant": "llama-3.1-8b-instant","llama-3.1-70b-8192-tool-use-preview": "llama3-groq-70b-8192-tool-use-preview"}self.groq_models = self._fetch_groq_models()261 unmodified linesreturn f"Error with Groq API: {str(e)}"def send_to_openai(prompt: str, api_key: str, model: str = "gpt-3.5-turbo") -> str:"""Send prompt to OpenAI API with better error handling."""try:import openaiopenai.api_key = api_keyresponse = openai.ChatCompletion.create(model=model,messages=[{"role": "system", "content": "You are a helpful assistant that provides detailed responses."},3 unmodified linesmax_tokens=500,top_p=0.95)return response.choices[0].message.contentexcept Exception as e:logging.error(f"OpenAI API error: {e}")return f"Error with OpenAI API: {str(e)}"25 unmodified lines2627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136261 unmodified lines3983994004014024034044054064074084094104114123 unmodified lines41641741841942042142242342442542642742842943025 unmodified lines}MODEL_CONTEXT_SIZES = {"Clipboard only": 4096,"OpenAI ChatGPT": {"gpt-3.5-turbo": 16385,"gpt-3.5-turbo-0125": 16385,"gpt-3.5-turbo-1106": 16385,"gpt-3.5-turbo-instruct": 4096,"gpt-4": 8192,"gpt-4-0314": 8192,"gpt-4-0613": 8192,"gpt-4-turbo": 128000,"gpt-4-turbo-2024-04-09": 128000,"gpt-4-turbo-preview": 128000,"gpt-4-0125-preview": 128000,"gpt-4-1106-preview": 128000,"gpt-4o": 128000,"gpt-4o-2024-11-20": 128000,"gpt-4o-2024-08-06": 128000,"gpt-4o-2024-05-13": 128000,"chatgpt-4o-latest": 128000,"gpt-4o-mini": 128000,"gpt-4o-mini-2024-07-18": 128000,"gpt-4o-realtime-preview": 128000,"gpt-4o-realtime-preview-2024-10-01": 128000,"gpt-4o-audio-preview": 128000,"gpt-4o-audio-preview-2024-10-01": 128000,"o1-preview": 128000,"o1-preview-2024-09-12": 128000,"o1-mini": 128000,"o1-mini-2024-09-12": 128000,},"HuggingFace Inference": {"microsoft/phi-3-mini-4k-instruct": 4096,"microsoft/Phi-3-mini-128k-instruct": 131072, # Added Phi-3 128k"HuggingFaceH4/zephyr-7b-beta": 8192,"deepseek-ai/DeepSeek-Coder-V2-Instruct": 8192,"meta-llama/Llama-3-8b-Instruct": 8192,"mistralai/Mistral-7B-Instruct-v0.3": 32768,"NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO": 32768,"microsoft/Phi-3.5-mini-instruct": 4096,"HuggingFaceTB/SmolLM2-1.7B-Instruct": 2048,"google/gemma-2-2b-it": 2048,"openai-community/gpt2": 1024,"microsoft/phi-2": 2048,"TinyLlama/TinyLlama-1.1B-Chat-v1.0": 2048},"Groq API": {"gemma2-9b-it": 8192,"gemma-7b-it": 8192,"llama-3.3-70b-versatile": 131072,"llama-3.1-70b-versatile": 131072, # Deprecated"llama-3.1-8b-instant": 131072,"llama-guard-3-8b": 8192,"llama3-70b-8192": 8192,"llama3-8b-8192": 8192,"mixtral-8x7b-32768": 32768,"llama3-groq-70b-8192-tool-use-preview": 8192,"llama3-groq-8b-8192-tool-use-preview": 8192,"llama-3.3-70b-specdec": 131072,"llama-3.1-70b-specdec": 131072,"llama-3.2-1b-preview": 131072,"llama-3.2-3b-preview": 131072,}}class ModelRegistry:def __init__(self):# HuggingFace Modelsself.hf_models = {"Phi-3 Mini 4K": "microsoft/phi-3-mini-4k-instruct","Phi-3 Mini 128k": "microsoft/Phi-3-mini-128k-instruct", # Added"Zephyr 7B Beta": "HuggingFaceH4/zephyr-7b-beta","DeepSeek Coder V2": "deepseek-ai/DeepSeek-Coder-V2-Instruct","Meta Llama 3.1 8B": "meta-llama/Llama-3-8b-Instruct","Meta Llama 3.1 70B": "meta-llama/Meta-Llama-3.1-70B-Instruct","Mixtral 7B": "mistralai/Mistral-7B-Instruct-v0.3","Nous-Hermes": "NousResearch/Nous-Hermes-2-Mixtral-8x7B-DPO","Cohere Command R+": "CohereForAI/c4ai-command-r-plus","Aya 23-35B": "CohereForAI/aya-23-35B","Phi-3.5 Mini": "microsoft/Phi-3.5-mini-instruct", # Added"SmolLM2 1.7B": "HuggingFaceTB/SmolLM2-1.7B-Instruct", # Added"Gemma 2 2B": "google/gemma-2-2b-it", # Added"GPT2": "openai-community/gpt2", # Added"Phi-2": "microsoft/phi-2", # Added"TinyLlama 1.1B": "TinyLlama/TinyLlama-1.1B-Chat-v1.0", # Added"Custom Model": "" # Keep for custom models}# Default Groq Modelsself.default_groq_models = { # Keep defaults in case fetching fails"gemma2-9b-it": "gemma2-9b-it","gemma-7b-it": "gemma-7b-it","llama-3.3-70b-versatile": "llama-3.3-70b-versatile","llama-3.1-70b-versatile": "llama-3.1-70b-versatile", # Deprecated"llama-3.1-8b-instant": "llama-3.1-8b-instant","llama-guard-3-8b": "llama-guard-3-8b","llama3-70b-8192": "llama3-70b-8192","llama3-8b-8192": "llama3-8b-8192","mixtral-8x7b-32768": "mixtral-8x7b-32768","llama3-groq-70b-8192-tool-use-preview": "llama3-groq-70b-8192-tool-use-preview","llama3-groq-8b-8192-tool-use-preview": "llama3-groq-8b-8192-tool-use-preview","llama-3.3-70b-specdec": "llama-3.3-70b-specdec","llama-3.1-70b-specdec": "llama-3.1-70b-specdec","llama-3.2-1b-preview": "llama-3.2-1b-preview","llama-3.2-3b-preview": "llama-3.2-3b-preview",}self.groq_models = self._fetch_groq_models()261 unmodified linesreturn f"Error with Groq API: {str(e)}"def send_to_openai(prompt: str, api_key: str, model: str = "gpt-3.5-turbo") -> str:"""Send prompt to OpenAI API using the new v1.0+ client format."""try:from openai import OpenAI # Import the new client# Initialize the client with the API keyclient = OpenAI(api_key=api_key)# Create chat completion using new formatresponse = client.chat.completions.create(model=model,messages=[{"role": "system", "content": "You are a helpful assistant that provides detailed responses."},3 unmodified linesmax_tokens=500,top_p=0.95)# Extract the response contentif response.choices and len(response.choices) > 0:return response.choices[0].message.contentelse:return "Error: No response generated"except ImportError:return "Error: Please install the latest version of openai package (pip install --upgrade openai)"except Exception as e:logging.error(f"OpenAI API error: {e}")return f"Error with OpenAI API: {str(e)}"
app.py+113-45