IFMedTechdemo commited on
Commit
7a2bcda
·
verified ·
1 Parent(s): f11dfc8

Fix model dropdown to show only names and improve output display

Browse files
Files changed (1) hide show
  1. app.py +106 -17
app.py CHANGED
@@ -7,15 +7,43 @@ 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 [
@@ -23,32 +51,50 @@ def get_available_models():
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
 
@@ -84,6 +130,20 @@ with gr.Blocks(title="Pollinations Text Generator") as demo:
84
  )
85
 
86
  with gr.Accordion("Advanced Settings", open=False):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  temperature_slider = gr.Slider(
88
  minimum=0,
89
  maximum=2,
@@ -114,25 +174,54 @@ with gr.Blocks(title="Pollinations Text Generator") as demo:
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
 
7
  try:
8
  response = requests.get("https://text.pollinations.ai/models")
9
  if response.status_code == 200:
10
+ models_data = response.json()
11
+ # Extract just the model names if API returns complex structure
12
+ if isinstance(models_data, list):
13
+ # If it's a list of strings, return as is
14
+ if all(isinstance(m, str) for m in models_data):
15
+ return models_data
16
+ # If it's a list of dicts, extract model names/id only
17
+ elif all(isinstance(m, dict) for m in models_data):
18
+ model_names = []
19
+ for m in models_data:
20
+ # Try to get 'name' or 'id' field, ignore everything else
21
+ if 'name' in m and isinstance(m['name'], str):
22
+ model_names.append(m['name'])
23
+ elif 'id' in m and isinstance(m['id'], str):
24
+ model_names.append(m['id'])
25
+ return model_names if model_names else [
26
+ "openai", "mistral", "mistral-large",
27
+ "claude-3.5-sonnet", "llama-3.3-70b", "gemini"
28
+ ]
29
+ # Fallback to default list
30
+ return [
31
+ "openai",
32
+ "mistral",
33
+ "mistral-large",
34
+ "claude-3.5-sonnet",
35
+ "llama-3.3-70b",
36
+ "gemini"
37
+ ]
38
  else:
39
+ # Fallback list of models
40
  return [
41
  "openai",
42
  "mistral",
43
  "mistral-large",
44
  "claude-3.5-sonnet",
45
+ "llama-3.3-70b",
46
+ "gemini"
47
  ]
48
  except:
49
  return [
 
51
  "mistral",
52
  "mistral-large",
53
  "claude-3.5-sonnet",
54
+ "llama-3.3-70b",
55
+ "gemini"
56
  ]
57
 
58
  # Function to generate text using Pollinations API
59
+ def generate_text(prompt, model, seed, system, temperature, max_tokens, top_p):
60
  if not prompt:
61
  return "Please enter a prompt."
62
 
63
  try:
64
+ # Prepare the API request using the same format as user's code
65
  url = "https://text.pollinations.ai/"
66
 
67
  # Build the query parameters
68
  params = {
69
  "model": model,
70
  "prompt": prompt,
 
 
 
71
  }
72
 
73
+ # Add optional parameters if provided
74
+ if seed:
75
+ params["seed"] = int(seed)
76
+ if system:
77
+ params["system"] = system
78
+ if temperature is not None:
79
+ params["temperature"] = temperature
80
+ if max_tokens:
81
+ params["max_tokens"] = int(max_tokens)
82
+ if top_p is not None:
83
+ params["top_p"] = top_p
84
+
85
  # Make the request
86
  response = requests.get(url, params=params)
87
 
88
  if response.status_code == 200:
89
+ result_text = response.text
90
+
91
+ # Try to parse as JSON for better formatting
92
+ try:
93
+ json_result = json.loads(result_text)
94
+ return f"```json\n{json.dumps(json_result, indent=2)}\n```"
95
+ except:
96
+ # Return as plain text if not JSON
97
+ return result_text
98
  else:
99
  return f"Error: API returned status code {response.status_code}\n{response.text}"
100
 
 
130
  )
131
 
132
  with gr.Accordion("Advanced Settings", open=False):
133
+ seed_input = gr.Number(
134
+ label="Seed (optional)",
135
+ value=None,
136
+ precision=0,
137
+ info="Random seed for reproducible results"
138
+ )
139
+
140
+ system_input = gr.Textbox(
141
+ label="System Prompt (optional)",
142
+ placeholder="Enter system instructions...",
143
+ lines=2,
144
+ info="System-level instructions for the model"
145
+ )
146
+
147
  temperature_slider = gr.Slider(
148
  minimum=0,
149
  maximum=2,
 
174
  generate_btn = gr.Button("Generate", variant="primary")
175
 
176
  with gr.Column():
177
+ output_display = gr.Markdown(
178
+ value="_Your generated text will appear here..._",
179
+ label="Generated Text"
 
180
  )
181
+
182
+ # Add a readonly textbox for easy copying
183
+ with gr.Accordion("Copy Output (Plain Text)", open=False):
184
+ output_copy = gr.Textbox(
185
+ label="Copyable Output",
186
+ lines=15,
187
+ show_copy_button=True,
188
+ interactive=False
189
+ )
190
 
191
  gr.Markdown(
192
  """
193
  ### About
194
  This Space uses the [Pollinations API](https://github.com/pollinations/pollinations) for text generation.
195
  The API supports multiple models and is free to use.
196
+
197
+ **Parameters:**
198
+ - **Model**: Choose from available AI models
199
+ - **Seed**: Set a random seed for reproducible outputs
200
+ - **System**: Provide system-level instructions
201
+ - **Temperature**: Control response creativity (0=deterministic, 2=very creative)
202
+ - **Max Tokens**: Set maximum response length
203
+ - **Top P**: Control diversity via nucleus sampling
204
  """
205
  )
206
 
207
  # Set up the generate button action
208
+ def generate_and_display(prompt, model, seed, system, temp, max_tok, top_p):
209
+ result = generate_text(prompt, model, seed, system, temp, max_tok, top_p)
210
+ # Return both markdown formatted and plain text versions
211
+ return result, result
212
+
213
  generate_btn.click(
214
+ fn=generate_and_display,
215
+ inputs=[
216
+ prompt_input,
217
+ model_dropdown,
218
+ seed_input,
219
+ system_input,
220
+ temperature_slider,
221
+ max_tokens_slider,
222
+ top_p_slider
223
+ ],
224
+ outputs=[output_display, output_copy]
225
  )
226
 
227
  # Launch the app