Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import os | |
| import requests | |
| import json | |
| API_KEY = os.getenv("access_token") | |
| def respond( | |
| message, | |
| max_tokens, | |
| temperature, | |
| top_p | |
| ): | |
| response = requests.post( | |
| url="https://api.featherless.ai/v1/completions", | |
| headers={ | |
| "Authorization": f"Bearer {API_KEY}", | |
| "Content-type": "application/json" | |
| }, | |
| json={ | |
| "model": "Qwen/Qwen3-8B", | |
| "prompt": message, | |
| "max_tokens": max_tokens, | |
| "temperature": temperature, | |
| "top_p": top_p | |
| } | |
| ) | |
| if response.status_code == 200: | |
| return response.json()["choices"][0]["text"] | |
| elif response.status_code == 429: | |
| try: | |
| output = response.json() | |
| return f"Error: {output['error']['code']}. Try submit your request again in a few seconds." | |
| except requests.exceptions.JSONDecodeError as e: | |
| return f"Error: {response}. Refresh the page and try again later, post the error on OLAT forum if your issue persists." | |
| else: | |
| try: | |
| output = response.json() | |
| return f"Error: {output['error']['message']}. Refresh the page and try again later, post the error on OLAT forum if your issue persists." | |
| except requests.exceptions.JSONDecodeError as e: | |
| return f"Error: {response}. Refresh the page and try again later, post the error on OLAT forum if your issue persists." | |
| demo = gr.Interface( | |
| fn=respond, | |
| title="Text Generation Playground", | |
| description="""Model: Qwen/Qwen3-8B, Max input length: 1500 characters\n | |
| Longer prompts take more time to process and may lead to timeouts or errors, Please keep your prompt short and concise.\n | |
| Prompts longer than 1500 characters will be truncated, which may lead to incomplete or unexpected responses.\n | |
| Try again or refresh the page if you encounter an error.""", | |
| inputs=[ | |
| gr.Textbox( | |
| lines=15, | |
| label="Input Prompt", | |
| max_length=1500, | |
| placeholder="""Copy-Paste your own alignment prompt here: | |
| E.g. # System-level prompt: You are a helpful assistant...\n | |
| # Query: Who painted the Mona Lisa?\n | |
| # Answer: The Mona Lisa was painted by Leonardo da Vinci, an Italian Renaissance artist, between 1503 and 1506, though some believe he may have continued working on it until 1517. It is one of the most famous and iconic works of art in history, known for its enigmatic expression and masterful technique.\n | |
| # Query: ...\n | |
| # Answer: | |
| # **Due to the limitations of the service, keep your prompt under 1500 characters.**""", | |
| ), | |
| gr.Slider(minimum=1, maximum=300, value=150, step=1, label="Max new tokens"), | |
| gr.Slider(minimum=0, maximum=2.0, value=1, step=0.01, label="Temperature"), | |
| gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05,label="Top-p (nucleus sampling)"), | |
| ], | |
| outputs=gr.Textbox(lines=20, label="Response") | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |