Spaces:
Sleeping
Sleeping
Add text-to-text generator with model selection using Pollinations API
Browse files
app.py
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import json
|
| 4 |
+
|
| 5 |
+
# Function to get available models from Pollinations API
|
| 6 |
+
def get_available_models():
|
| 7 |
+
try:
|
| 8 |
+
response = requests.get("https://text.pollinations.ai/models")
|
| 9 |
+
if response.status_code == 200:
|
| 10 |
+
return response.json()
|
| 11 |
+
else:
|
| 12 |
+
# Fallback list of models based on API documentation
|
| 13 |
+
return [
|
| 14 |
+
"openai",
|
| 15 |
+
"mistral",
|
| 16 |
+
"mistral-large",
|
| 17 |
+
"claude-3.5-sonnet",
|
| 18 |
+
"llama-3.3-70b"
|
| 19 |
+
]
|
| 20 |
+
except:
|
| 21 |
+
return [
|
| 22 |
+
"openai",
|
| 23 |
+
"mistral",
|
| 24 |
+
"mistral-large",
|
| 25 |
+
"claude-3.5-sonnet",
|
| 26 |
+
"llama-3.3-70b"
|
| 27 |
+
]
|
| 28 |
+
|
| 29 |
+
# Function to generate text using Pollinations API
|
| 30 |
+
def generate_text(prompt, model, temperature, max_tokens, top_p):
|
| 31 |
+
if not prompt:
|
| 32 |
+
return "Please enter a prompt."
|
| 33 |
+
|
| 34 |
+
try:
|
| 35 |
+
# Prepare the API request
|
| 36 |
+
url = "https://text.pollinations.ai/"
|
| 37 |
+
|
| 38 |
+
# Build the query parameters
|
| 39 |
+
params = {
|
| 40 |
+
"model": model,
|
| 41 |
+
"prompt": prompt,
|
| 42 |
+
"temperature": temperature,
|
| 43 |
+
"max_tokens": int(max_tokens),
|
| 44 |
+
"top_p": top_p
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
# Make the request
|
| 48 |
+
response = requests.get(url, params=params)
|
| 49 |
+
|
| 50 |
+
if response.status_code == 200:
|
| 51 |
+
return response.text
|
| 52 |
+
else:
|
| 53 |
+
return f"Error: API returned status code {response.status_code}\n{response.text}"
|
| 54 |
+
|
| 55 |
+
except Exception as e:
|
| 56 |
+
return f"Error: {str(e)}"
|
| 57 |
+
|
| 58 |
+
# Get available models
|
| 59 |
+
available_models = get_available_models()
|
| 60 |
+
|
| 61 |
+
# Create Gradio interface
|
| 62 |
+
with gr.Blocks(title="Pollinations Text Generator") as demo:
|
| 63 |
+
gr.Markdown(
|
| 64 |
+
"""
|
| 65 |
+
# 🌸 Pollinations Text Generator
|
| 66 |
+
Generate text using various AI models via the Pollinations API.
|
| 67 |
+
Select a model and provide a prompt to get started!
|
| 68 |
+
"""
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
with gr.Row():
|
| 72 |
+
with gr.Column():
|
| 73 |
+
prompt_input = gr.Textbox(
|
| 74 |
+
label="Prompt",
|
| 75 |
+
placeholder="Enter your text prompt here...",
|
| 76 |
+
lines=5
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
model_dropdown = gr.Dropdown(
|
| 80 |
+
choices=available_models,
|
| 81 |
+
label="Model",
|
| 82 |
+
value=available_models[0] if available_models else "openai",
|
| 83 |
+
info="Select the AI model to use for text generation"
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
with gr.Accordion("Advanced Settings", open=False):
|
| 87 |
+
temperature_slider = gr.Slider(
|
| 88 |
+
minimum=0,
|
| 89 |
+
maximum=2,
|
| 90 |
+
value=0.7,
|
| 91 |
+
step=0.1,
|
| 92 |
+
label="Temperature",
|
| 93 |
+
info="Controls randomness (higher = more creative)"
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
max_tokens_slider = gr.Slider(
|
| 97 |
+
minimum=1,
|
| 98 |
+
maximum=2048,
|
| 99 |
+
value=512,
|
| 100 |
+
step=1,
|
| 101 |
+
label="Max Tokens",
|
| 102 |
+
info="Maximum length of the generated text"
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
top_p_slider = gr.Slider(
|
| 106 |
+
minimum=0,
|
| 107 |
+
maximum=1,
|
| 108 |
+
value=0.9,
|
| 109 |
+
step=0.05,
|
| 110 |
+
label="Top P",
|
| 111 |
+
info="Nucleus sampling parameter"
|
| 112 |
+
)
|
| 113 |
+
|
| 114 |
+
generate_btn = gr.Button("Generate", variant="primary")
|
| 115 |
+
|
| 116 |
+
with gr.Column():
|
| 117 |
+
output_text = gr.Textbox(
|
| 118 |
+
label="Generated Text",
|
| 119 |
+
lines=15,
|
| 120 |
+
show_copy_button=True
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
gr.Markdown(
|
| 124 |
+
"""
|
| 125 |
+
### About
|
| 126 |
+
This Space uses the [Pollinations API](https://github.com/pollinations/pollinations) for text generation.
|
| 127 |
+
The API supports multiple models and is free to use.
|
| 128 |
+
"""
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
# Set up the generate button action
|
| 132 |
+
generate_btn.click(
|
| 133 |
+
fn=generate_text,
|
| 134 |
+
inputs=[prompt_input, model_dropdown, temperature_slider, max_tokens_slider, top_p_slider],
|
| 135 |
+
outputs=output_text
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
# Launch the app
|
| 139 |
+
if __name__ == "__main__":
|
| 140 |
+
demo.launch()
|