Spaces:
Sleeping
Sleeping
xlr8harder
commited on
Commit
·
1ce16f9
1
Parent(s):
5458003
add provider require_parameters option to exclude providers that do not support completions api to avoid random errors
Browse files
app.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
| 3 |
import os
|
| 4 |
|
| 5 |
default_prompt = "The best thing about being a cat is"
|
|
@@ -7,23 +8,54 @@ default_prompt = "The best thing about being a cat is"
|
|
| 7 |
def generate_completion(api_key, model, prompt, temperature, repetition_penalty, stop_phrase, max_tokens):
|
| 8 |
if not api_key:
|
| 9 |
return "Please enter your OpenRouter API key."
|
| 10 |
-
|
| 11 |
try:
|
| 12 |
-
#
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
)
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
except Exception as e:
|
| 28 |
return f"An error occurred: {str(e)}"
|
| 29 |
|
|
@@ -35,12 +67,12 @@ def clear_fields():
|
|
| 35 |
return "", ""
|
| 36 |
|
| 37 |
with gr.Blocks(theme=gr.themes.Soft()) as iface:
|
| 38 |
-
gr.Markdown("#
|
| 39 |
-
|
| 40 |
# API Key input at the top
|
| 41 |
api_key_input = gr.Textbox(
|
| 42 |
-
label="OpenRouter API Key",
|
| 43 |
-
type="password",
|
| 44 |
placeholder="Enter your OpenRouter API key"
|
| 45 |
)
|
| 46 |
|
|
@@ -49,7 +81,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as iface:
|
|
| 49 |
prompt_input = gr.Textbox(label="Prompt", lines=6, value="The best thing about being a cat is")
|
| 50 |
with gr.Column(scale=1):
|
| 51 |
model_input = gr.Textbox(
|
| 52 |
-
label="Model",
|
| 53 |
value="meta-llama/llama-3.1-405b",
|
| 54 |
placeholder="Enter model name"
|
| 55 |
)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
import os
|
| 5 |
|
| 6 |
default_prompt = "The best thing about being a cat is"
|
|
|
|
| 8 |
def generate_completion(api_key, model, prompt, temperature, repetition_penalty, stop_phrase, max_tokens):
|
| 9 |
if not api_key:
|
| 10 |
return "Please enter your OpenRouter API key."
|
| 11 |
+
|
| 12 |
try:
|
| 13 |
+
# Prepare the request payload
|
| 14 |
+
payload = {
|
| 15 |
+
"model": model,
|
| 16 |
+
"prompt": prompt,
|
| 17 |
+
"temperature": temperature,
|
| 18 |
+
"frequency_penalty": repetition_penalty,
|
| 19 |
+
"max_tokens": max_tokens,
|
| 20 |
+
"provider": {
|
| 21 |
+
"require_parameters": True
|
| 22 |
+
}
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
# Add stop phrase if provided
|
| 26 |
+
if stop_phrase:
|
| 27 |
+
payload["stop"] = [stop_phrase]
|
| 28 |
+
|
| 29 |
+
# Prepare headers
|
| 30 |
+
headers = {
|
| 31 |
+
"Authorization": f"Bearer {api_key}",
|
| 32 |
+
"Content-Type": "application/json"
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
# Make the API request
|
| 36 |
+
response = requests.post(
|
| 37 |
+
"https://openrouter.ai/api/v1/completions",
|
| 38 |
+
headers=headers,
|
| 39 |
+
json=payload,
|
| 40 |
+
timeout=60
|
| 41 |
)
|
| 42 |
+
|
| 43 |
+
# Check if request was successful
|
| 44 |
+
response.raise_for_status()
|
| 45 |
+
|
| 46 |
+
# Parse the response
|
| 47 |
+
result = response.json()
|
| 48 |
+
|
| 49 |
+
# Extract the completion text
|
| 50 |
+
if "choices" in result and len(result["choices"]) > 0:
|
| 51 |
+
return result["choices"][0]["text"].strip()
|
| 52 |
+
else:
|
| 53 |
+
return "No completion generated."
|
| 54 |
+
|
| 55 |
+
except requests.exceptions.RequestException as e:
|
| 56 |
+
return f"Request error: {str(e)}"
|
| 57 |
+
except json.JSONDecodeError as e:
|
| 58 |
+
return f"JSON decode error: {str(e)}"
|
| 59 |
except Exception as e:
|
| 60 |
return f"An error occurred: {str(e)}"
|
| 61 |
|
|
|
|
| 67 |
return "", ""
|
| 68 |
|
| 69 |
with gr.Blocks(theme=gr.themes.Soft()) as iface:
|
| 70 |
+
gr.Markdown("# Base Model Completion Interface via OpenRouter")
|
| 71 |
+
|
| 72 |
# API Key input at the top
|
| 73 |
api_key_input = gr.Textbox(
|
| 74 |
+
label="OpenRouter API Key",
|
| 75 |
+
type="password",
|
| 76 |
placeholder="Enter your OpenRouter API key"
|
| 77 |
)
|
| 78 |
|
|
|
|
| 81 |
prompt_input = gr.Textbox(label="Prompt", lines=6, value="The best thing about being a cat is")
|
| 82 |
with gr.Column(scale=1):
|
| 83 |
model_input = gr.Textbox(
|
| 84 |
+
label="Model",
|
| 85 |
value="meta-llama/llama-3.1-405b",
|
| 86 |
placeholder="Enter model name"
|
| 87 |
)
|