Spaces:
Running
Running
xlr8harder
commited on
Commit
·
0712223
1
Parent(s):
18efae6
update to openrouter
Browse files
app.py
CHANGED
|
@@ -4,17 +4,19 @@ import os
|
|
| 4 |
|
| 5 |
default_prompt = "The best thing about being a cat is"
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
api_key=api_key,
|
| 12 |
-
)
|
| 13 |
-
|
| 14 |
-
def generate_completion(prompt, temperature, repetition_penalty, stop_phrase, max_tokens):
|
| 15 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
completion = client.completions.create(
|
| 17 |
-
model="meta-llama/
|
| 18 |
prompt=prompt,
|
| 19 |
temperature=temperature,
|
| 20 |
frequency_penalty=repetition_penalty,
|
|
@@ -33,8 +35,15 @@ def clear_fields():
|
|
| 33 |
return "", ""
|
| 34 |
|
| 35 |
with gr.Blocks(theme=gr.themes.Soft()) as iface:
|
| 36 |
-
gr.Markdown("# Llama 3.1 405B Completion Interface")
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
with gr.Row():
|
| 39 |
with gr.Column(scale=2):
|
| 40 |
prompt_input = gr.Textbox(label="Prompt", lines=6, value="The best thing about being a cat is")
|
|
@@ -43,35 +52,29 @@ with gr.Blocks(theme=gr.themes.Soft()) as iface:
|
|
| 43 |
repetition_penalty_slider = gr.Slider(minimum=0, maximum=2, value=0.1, step=0.1, label="Repetition Penalty")
|
| 44 |
max_tokens_slider = gr.Slider(minimum=1, maximum=4000, value=250, step=1, label="Max Tokens")
|
| 45 |
stop_phrase_input = gr.Textbox(label="Stop Phrase", placeholder="Enter stop phrase (optional)")
|
| 46 |
-
|
| 47 |
with gr.Row():
|
| 48 |
generate_button = gr.Button("Generate Completion")
|
| 49 |
append_button = gr.Button("Append Completion to Prompt")
|
| 50 |
clear_button = gr.Button("Clear All Fields")
|
| 51 |
-
|
| 52 |
output_text = gr.Textbox(label="Generated Completion", lines=10)
|
| 53 |
-
|
| 54 |
generate_button.click(
|
| 55 |
generate_completion,
|
| 56 |
-
inputs=[prompt_input, temperature_slider, repetition_penalty_slider, stop_phrase_input, max_tokens_slider],
|
| 57 |
outputs=output_text
|
| 58 |
)
|
| 59 |
-
|
| 60 |
append_button.click(
|
| 61 |
append_completion,
|
| 62 |
inputs=[prompt_input, output_text],
|
| 63 |
outputs=[prompt_input, output_text]
|
| 64 |
)
|
| 65 |
-
|
| 66 |
clear_button.click(
|
| 67 |
clear_fields,
|
| 68 |
outputs=[prompt_input, output_text]
|
| 69 |
)
|
| 70 |
-
|
| 71 |
-
gr.Markdown("""
|
| 72 |
-
---
|
| 73 |
-
This interface is powered by the Llama 3.1 405B base model, served by [Hyperbolic](https://hyperbolic.xyz), The Open Access AI Cloud.
|
| 74 |
-
Thank you to Hyperbolic for making this base model available!
|
| 75 |
-
""")
|
| 76 |
|
| 77 |
iface.launch(share=True)
|
|
|
|
| 4 |
|
| 5 |
default_prompt = "The best thing about being a cat is"
|
| 6 |
|
| 7 |
+
def generate_completion(api_key, 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 |
+
# Initialize the OpenAI client with OpenRouter
|
| 13 |
+
client = OpenAI(
|
| 14 |
+
base_url="https://openrouter.ai/api/v1",
|
| 15 |
+
api_key=api_key,
|
| 16 |
+
)
|
| 17 |
+
|
| 18 |
completion = client.completions.create(
|
| 19 |
+
model="meta-llama/llama-3.1-405b",
|
| 20 |
prompt=prompt,
|
| 21 |
temperature=temperature,
|
| 22 |
frequency_penalty=repetition_penalty,
|
|
|
|
| 35 |
return "", ""
|
| 36 |
|
| 37 |
with gr.Blocks(theme=gr.themes.Soft()) as iface:
|
| 38 |
+
gr.Markdown("# Llama 3.1 405B Completion Interface via OpenRouter")
|
| 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 |
+
|
| 47 |
with gr.Row():
|
| 48 |
with gr.Column(scale=2):
|
| 49 |
prompt_input = gr.Textbox(label="Prompt", lines=6, value="The best thing about being a cat is")
|
|
|
|
| 52 |
repetition_penalty_slider = gr.Slider(minimum=0, maximum=2, value=0.1, step=0.1, label="Repetition Penalty")
|
| 53 |
max_tokens_slider = gr.Slider(minimum=1, maximum=4000, value=250, step=1, label="Max Tokens")
|
| 54 |
stop_phrase_input = gr.Textbox(label="Stop Phrase", placeholder="Enter stop phrase (optional)")
|
| 55 |
+
|
| 56 |
with gr.Row():
|
| 57 |
generate_button = gr.Button("Generate Completion")
|
| 58 |
append_button = gr.Button("Append Completion to Prompt")
|
| 59 |
clear_button = gr.Button("Clear All Fields")
|
| 60 |
+
|
| 61 |
output_text = gr.Textbox(label="Generated Completion", lines=10)
|
| 62 |
+
|
| 63 |
generate_button.click(
|
| 64 |
generate_completion,
|
| 65 |
+
inputs=[api_key_input, prompt_input, temperature_slider, repetition_penalty_slider, stop_phrase_input, max_tokens_slider],
|
| 66 |
outputs=output_text
|
| 67 |
)
|
| 68 |
+
|
| 69 |
append_button.click(
|
| 70 |
append_completion,
|
| 71 |
inputs=[prompt_input, output_text],
|
| 72 |
outputs=[prompt_input, output_text]
|
| 73 |
)
|
| 74 |
+
|
| 75 |
clear_button.click(
|
| 76 |
clear_fields,
|
| 77 |
outputs=[prompt_input, output_text]
|
| 78 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
iface.launch(share=True)
|