Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import requests | |
| import urllib.parse | |
| import json | |
| def get_pollinations_response(prompt, model="openai", seed=42, use_json=False, system_prompt=""): | |
| """ | |
| Function to call Pollinations AI API | |
| """ | |
| params = { | |
| "model": model, | |
| "seed": seed, | |
| } | |
| # Add optional parameters | |
| if use_json: | |
| params["json"] = "true" | |
| if system_prompt: | |
| params["system"] = system_prompt | |
| # Encode the prompt | |
| encoded_prompt = urllib.parse.quote(prompt) | |
| # Build URL | |
| url = f"https://text.pollinations.ai/{encoded_prompt}" | |
| try: | |
| response = requests.get(url, params=params) | |
| response.raise_for_status() | |
| if use_json: | |
| # Parse JSON response | |
| try: | |
| data = json.loads(response.text) | |
| return json.dumps(data, indent=2) | |
| except json.JSONDecodeError: | |
| return f"Error: API returned invalid JSON.\nRaw response: {response.text}" | |
| else: | |
| return response.text | |
| except requests.exceptions.RequestException as e: | |
| return f"Error fetching text: {e}" | |
| # Create Gradio interface | |
| with gr.Blocks(title="AI Text API") as demo: | |
| gr.Markdown("# AI Text Generator") | |
| gr.Markdown("Generate text using AI API with customizable parameters") | |
| with gr.Row(): | |
| with gr.Column(): | |
| prompt_input = gr.Textbox( | |
| label="Prompt", | |
| placeholder="Enter your prompt here...", | |
| lines=3 | |
| ) | |
| with gr.Row(): | |
| model_dropdown = gr.Dropdown( | |
| choices=["openai", "mistral","openai-reasoning", "openai-fast","chickytutor"], | |
| value="openai", | |
| label="Model" | |
| ) | |
| seed_number = gr.Number( | |
| value=42, | |
| label="Seed", | |
| precision=0 | |
| ) | |
| system_input = gr.Textbox( | |
| label="System Prompt (Optional)", | |
| placeholder="e.g., Explain things like I'm five.", | |
| lines=2 | |
| ) | |
| json_checkbox = gr.Checkbox( | |
| label="Return as JSON", | |
| value=False | |
| ) | |
| submit_btn = gr.Button("Generate", variant="primary") | |
| with gr.Column(): | |
| output_text = gr.Textbox( | |
| label="Response", | |
| lines=15, | |
| max_lines=20 | |
| ) | |
| # Event handler | |
| submit_btn.click( | |
| fn=get_pollinations_response, | |
| inputs=[prompt_input, model_dropdown, seed_number, json_checkbox, system_input], | |
| outputs=output_text | |
| ) | |
| # Also trigger on Enter key press in prompt | |
| prompt_input.submit( | |
| fn=get_pollinations_response, | |
| inputs=[prompt_input, model_dropdown, seed_number, json_checkbox, system_input], | |
| outputs=output_text | |
| ) | |
| # Examples | |
| gr.Examples( | |
| examples=[ | |
| ["Explain the theory of relativity simply", "openai", 42, False, ""], | |
| ["Write a haiku about coding", "openai", 42, False, "You are a creative poet"], | |
| ["What is machine learning?", "openai", 42, False, "Explain things like I'm five."], | |
| ], | |
| inputs=[prompt_input, model_dropdown, seed_number, json_checkbox, system_input] | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() | |