Spaces:
Sleeping
Sleeping
File size: 3,519 Bytes
884ae73 f91f25c 884ae73 f91f25c 884ae73 f91f25c 884ae73 f91f25c 7a2bcda f91f25c 884ae73 f91f25c 884ae73 c35a45e 2190920 c35a45e 884ae73 f91f25c 884ae73 f91f25c 6176c21 f91f25c 884ae73 f91f25c 884ae73 f91f25c 884ae73 f91f25c 884ae73 f91f25c 884ae73 f91f25c 7a2bcda f91f25c 7a2bcda f91f25c 884ae73 f91f25c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
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()
|