akhaliq HF Staff commited on
Commit
c2c7255
Β·
1 Parent(s): ee44e03

add inference provider code

Browse files
Files changed (2) hide show
  1. anycoder_app/deploy.py +396 -6
  2. anycoder_app/ui.py +56 -5
anycoder_app/deploy.py CHANGED
@@ -2030,9 +2030,301 @@ def import_space_from_hf(space_id: str) -> Tuple[str, str, str, str]:
2030
  return status, code, language, space_url
2031
 
2032
 
2033
- def import_model_from_hf(model_id: str) -> Tuple[str, str, str, str]:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2034
  """
2035
  Import a HuggingFace model by ID and extract code snippet.
 
 
 
 
 
2036
 
2037
  Returns: (status, code, language, model_url)
2038
  """
@@ -2042,13 +2334,111 @@ def import_model_from_hf(model_id: str) -> Tuple[str, str, str, str]:
2042
  # Build model URL
2043
  model_url = f"https://huggingface.co/{model_id}"
2044
 
2045
- # Use existing import_repo_to_app function
2046
- status, code, _ = import_repo_to_app(model_url)
 
 
 
 
2047
 
2048
- # Determine language - default to python for model imports
2049
- language = "gradio" # Default framework for model demos
2050
 
2051
- return status, code, language, model_url
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2052
 
2053
 
2054
  def import_repo_to_app(url: str, framework: str = "Gradio") -> Tuple[str, str, str]:
 
2030
  return status, code, language, space_url
2031
 
2032
 
2033
+ def _generate_inference_code_template(model_id: str, pipeline_tag: Optional[str], has_inference_providers: bool) -> Optional[str]:
2034
+ """
2035
+ Generate inference provider code template based on model's pipeline tag.
2036
+
2037
+ Args:
2038
+ model_id: The HuggingFace model ID
2039
+ pipeline_tag: The model's pipeline tag (e.g., "text-generation", "text-to-image")
2040
+ has_inference_providers: Whether the model has inference providers available
2041
+
2042
+ Returns:
2043
+ Generated code snippet or None
2044
+ """
2045
+ if not has_inference_providers:
2046
+ return None
2047
+
2048
+ # Map pipeline tags to code templates based on HuggingFace Inference Providers docs
2049
+ # https://huggingface.co/docs/inference-providers
2050
+
2051
+ # Chat Completion / Text Generation models
2052
+ if pipeline_tag in ["text-generation", "conversational"]:
2053
+ return f'''import os
2054
+ from huggingface_hub import InferenceClient
2055
+
2056
+ client = InferenceClient(
2057
+ api_key=os.environ["HF_TOKEN"],
2058
+ )
2059
+
2060
+ completion = client.chat.completions.create(
2061
+ model="{model_id}",
2062
+ messages=[
2063
+ {{
2064
+ "role": "user",
2065
+ "content": "What is the capital of France?"
2066
+ }}
2067
+ ],
2068
+ )
2069
+
2070
+ print(completion.choices[0].message)'''
2071
+
2072
+ # Vision-Language Models (Image-Text to Text)
2073
+ elif pipeline_tag in ["image-text-to-text", "visual-question-answering"]:
2074
+ return f'''import os
2075
+ from huggingface_hub import InferenceClient
2076
+
2077
+ client = InferenceClient(
2078
+ api_key=os.environ["HF_TOKEN"],
2079
+ )
2080
+
2081
+ completion = client.chat.completions.create(
2082
+ model="{model_id}",
2083
+ messages=[
2084
+ {{
2085
+ "role": "user",
2086
+ "content": [
2087
+ {{
2088
+ "type": "text",
2089
+ "text": "Describe this image in one sentence."
2090
+ }},
2091
+ {{
2092
+ "type": "image_url",
2093
+ "image_url": {{
2094
+ "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg"
2095
+ }}
2096
+ }}
2097
+ ]
2098
+ }}
2099
+ ],
2100
+ )
2101
+
2102
+ print(completion.choices[0].message)'''
2103
+
2104
+ # Text to Image models
2105
+ elif pipeline_tag == "text-to-image":
2106
+ return f'''import os
2107
+ from huggingface_hub import InferenceClient
2108
+
2109
+ client = InferenceClient(
2110
+ api_key=os.environ["HF_TOKEN"],
2111
+ )
2112
+
2113
+ # output is a PIL.Image object
2114
+ image = client.text_to_image(
2115
+ "Astronaut riding a horse",
2116
+ model="{model_id}",
2117
+ )
2118
+
2119
+ # Save the image
2120
+ image.save("output.png")'''
2121
+
2122
+ # Text to Video models
2123
+ elif pipeline_tag == "text-to-video":
2124
+ return f'''import os
2125
+ from huggingface_hub import InferenceClient
2126
+
2127
+ client = InferenceClient(
2128
+ api_key=os.environ["HF_TOKEN"],
2129
+ )
2130
+
2131
+ video = client.text_to_video(
2132
+ "A young man walking on the street",
2133
+ model="{model_id}",
2134
+ )
2135
+
2136
+ # Save the video
2137
+ with open("output.mp4", "wb") as f:
2138
+ f.write(video)'''
2139
+
2140
+ # Image to Image models
2141
+ elif pipeline_tag == "image-to-image":
2142
+ return f'''import os
2143
+ from huggingface_hub import InferenceClient
2144
+ from PIL import Image
2145
+
2146
+ client = InferenceClient(
2147
+ api_key=os.environ["HF_TOKEN"],
2148
+ )
2149
+
2150
+ # Load input image
2151
+ input_image = Image.open("input.jpg")
2152
+
2153
+ # output is a PIL.Image object
2154
+ output_image = client.image_to_image(
2155
+ input_image,
2156
+ model="{model_id}",
2157
+ prompt="Make it more vibrant"
2158
+ )
2159
+
2160
+ # Save the output
2161
+ output_image.save("output.png")'''
2162
+
2163
+ # Text to Speech models
2164
+ elif pipeline_tag == "text-to-speech":
2165
+ return f'''import os
2166
+ from huggingface_hub import InferenceClient
2167
+
2168
+ client = InferenceClient(
2169
+ api_key=os.environ["HF_TOKEN"],
2170
+ )
2171
+
2172
+ audio = client.text_to_speech(
2173
+ "Hello world",
2174
+ model="{model_id}",
2175
+ )
2176
+
2177
+ # Save the audio
2178
+ with open("output.mp3", "wb") as f:
2179
+ f.write(audio)'''
2180
+
2181
+ # Automatic Speech Recognition
2182
+ elif pipeline_tag == "automatic-speech-recognition":
2183
+ return f'''import os
2184
+ from huggingface_hub import InferenceClient
2185
+
2186
+ client = InferenceClient(
2187
+ api_key=os.environ["HF_TOKEN"],
2188
+ )
2189
+
2190
+ with open("audio.mp3", "rb") as f:
2191
+ audio_data = f.read()
2192
+
2193
+ result = client.automatic_speech_recognition(
2194
+ audio_data,
2195
+ model="{model_id}",
2196
+ )
2197
+
2198
+ print(result)'''
2199
+
2200
+ # Feature Extraction / Embeddings
2201
+ elif pipeline_tag == "feature-extraction":
2202
+ return f'''import os
2203
+ from huggingface_hub import InferenceClient
2204
+
2205
+ client = InferenceClient(
2206
+ api_key=os.environ["HF_TOKEN"],
2207
+ )
2208
+
2209
+ embeddings = client.feature_extraction(
2210
+ "Hello world",
2211
+ model="{model_id}",
2212
+ )
2213
+
2214
+ print(embeddings)'''
2215
+
2216
+ # Default: try chat completion for conversational models
2217
+ else:
2218
+ # If it has inference providers but unknown task, try chat completion
2219
+ return f'''import os
2220
+ from huggingface_hub import InferenceClient
2221
+
2222
+ client = InferenceClient(
2223
+ api_key=os.environ["HF_TOKEN"],
2224
+ )
2225
+
2226
+ completion = client.chat.completions.create(
2227
+ model="{model_id}",
2228
+ messages=[
2229
+ {{
2230
+ "role": "user",
2231
+ "content": "Hello, how are you?"
2232
+ }}
2233
+ ],
2234
+ )
2235
+
2236
+ print(completion.choices[0].message)'''
2237
+
2238
+
2239
+ def _fetch_inference_provider_code(model_id: str) -> Optional[str]:
2240
+ """
2241
+ Fetch inference provider information from HuggingFace API and generate code template.
2242
+
2243
+ Args:
2244
+ model_id: The HuggingFace model ID (e.g., "moonshotai/Kimi-K2-Thinking")
2245
+
2246
+ Returns:
2247
+ The code snippet if model has inference providers, None otherwise
2248
+ """
2249
+ try:
2250
+ # Fetch trending models data from HuggingFace API
2251
+ response = requests.get("https://huggingface.co/api/trending", timeout=10)
2252
+
2253
+ if response.status_code != 200:
2254
+ print(f"Failed to fetch trending models API: HTTP {response.status_code}")
2255
+ return None
2256
+
2257
+ trending_data = response.json()
2258
+ recently_trending = trending_data.get("recentlyTrending", [])
2259
+
2260
+ # Find the specific model in trending data
2261
+ model_info = None
2262
+ for item in recently_trending:
2263
+ repo_data = item.get("repoData", {})
2264
+ if repo_data.get("id") == model_id:
2265
+ model_info = repo_data
2266
+ break
2267
+
2268
+ # If not found in trending, try to get model info directly from API
2269
+ if not model_info:
2270
+ try:
2271
+ api = HfApi()
2272
+ info = api.model_info(model_id)
2273
+ pipeline_tag = getattr(info, "pipeline_tag", None)
2274
+
2275
+ # Check if model has inference providers via model info
2276
+ # Note: The direct API might not have availableInferenceProviders
2277
+ # In this case, we'll generate a generic template
2278
+ has_inference = pipeline_tag is not None
2279
+
2280
+ if has_inference:
2281
+ return _generate_inference_code_template(model_id, pipeline_tag, True)
2282
+ except Exception as e:
2283
+ print(f"Could not fetch model info for {model_id}: {e}")
2284
+ return None
2285
+ else:
2286
+ # Extract pipeline tag and inference providers info
2287
+ pipeline_tag = model_info.get("pipeline_tag")
2288
+ inference_providers = model_info.get("availableInferenceProviders", [])
2289
+ has_inference_providers = len(inference_providers) > 0
2290
+
2291
+ # Generate code template based on pipeline tag
2292
+ return _generate_inference_code_template(model_id, pipeline_tag, has_inference_providers)
2293
+
2294
+ return None
2295
+
2296
+ except Exception as e:
2297
+ print(f"Error fetching inference provider code: {e}")
2298
+ return None
2299
+
2300
+
2301
+ # Global storage for code alternatives (used when both inference and local code are available)
2302
+ _model_code_alternatives = {}
2303
+
2304
+
2305
+ def store_model_code_alternatives(model_id: str, inference_code: Optional[str], local_code: Optional[str]):
2306
+ """Store both code alternatives for a model for later retrieval."""
2307
+ global _model_code_alternatives
2308
+ _model_code_alternatives[model_id] = {
2309
+ 'inference': inference_code,
2310
+ 'local': local_code
2311
+ }
2312
+
2313
+
2314
+ def get_model_code_alternatives(model_id: str) -> Dict[str, Optional[str]]:
2315
+ """Retrieve stored code alternatives for a model."""
2316
+ global _model_code_alternatives
2317
+ return _model_code_alternatives.get(model_id, {'inference': None, 'local': None})
2318
+
2319
+
2320
+ def import_model_from_hf(model_id: str, prefer_local: bool = False) -> Tuple[str, str, str, str]:
2321
  """
2322
  Import a HuggingFace model by ID and extract code snippet.
2323
+ Tries to fetch both inference provider code and transformers/diffusers code from README.
2324
+
2325
+ Args:
2326
+ model_id: The HuggingFace model ID
2327
+ prefer_local: If True and both options available, return local code instead of inference code
2328
 
2329
  Returns: (status, code, language, model_url)
2330
  """
 
2334
  # Build model URL
2335
  model_url = f"https://huggingface.co/{model_id}"
2336
 
2337
+ # Try to fetch both types of code
2338
+ inference_code = _fetch_inference_provider_code(model_id)
2339
+
2340
+ # Also try to extract transformers/diffusers code from README
2341
+ readme_status, readme_code, _ = import_repo_to_app(model_url)
2342
+ has_readme_code = readme_code and ("transformers" in readme_code or "diffusers" in readme_code)
2343
 
2344
+ # Store both alternatives for later switching
2345
+ store_model_code_alternatives(model_id, inference_code, readme_code if has_readme_code else None)
2346
 
2347
+ # Build status message and code based on what's available
2348
+ if inference_code and has_readme_code:
2349
+ # Both available - provide choice
2350
+ if prefer_local:
2351
+ status = f"""βœ… **Found multiple code options for `{model_id}`**
2352
+
2353
+ **Currently showing:** Local Transformers/Diffusers Code (Option 2) πŸ’»
2354
+
2355
+ **Option 1: Inference Provider Code (Serverless)** ⚑
2356
+ - Uses HuggingFace Inference API (serverless, pay-per-use)
2357
+ - No GPU required, instant startup
2358
+ - Requires `HF_TOKEN` environment variable
2359
+
2360
+ **Option 2: Local Transformers/Diffusers Code (Currently Active)** πŸ’»
2361
+ - Runs locally on your hardware
2362
+ - Requires GPU for optimal performance
2363
+ - Full control over model parameters
2364
+
2365
+ ---
2366
+
2367
+ To switch to inference provider code, click the button below or ask: "Show me the inference provider code instead"
2368
+ """
2369
+ code = readme_code
2370
+ else:
2371
+ status = f"""βœ… **Found multiple code options for `{model_id}`**
2372
+
2373
+ **Currently showing:** Inference Provider Code (Option 1) ⚑ *Recommended*
2374
+
2375
+ **Option 1: Inference Provider Code (Serverless - Currently Active)** ⚑
2376
+ - Uses HuggingFace Inference API (serverless, pay-per-use)
2377
+ - No GPU required, instant startup
2378
+ - Requires `HF_TOKEN` environment variable
2379
+
2380
+ **Option 2: Local Transformers/Diffusers Code** πŸ’»
2381
+ - Runs locally on your hardware
2382
+ - Requires GPU for optimal performance
2383
+ - Full control over model parameters
2384
+
2385
+ ---
2386
+
2387
+ To switch to local transformers/diffusers code, click the button below or ask: "Show me the local transformers code instead"
2388
+ """
2389
+ code = inference_code
2390
+
2391
+ language = "gradio"
2392
+ return status, code, language, model_url
2393
+
2394
+ elif inference_code:
2395
+ # Only inference provider code available
2396
+ status = f"βœ… Imported inference provider code for `{model_id}` (serverless inference)"
2397
+ language = "gradio"
2398
+ return status, inference_code, language, model_url
2399
+
2400
+ elif has_readme_code:
2401
+ # Only README code available
2402
+ status = f"βœ… Imported transformers/diffusers code from README for `{model_id}` (local inference)"
2403
+ language = "gradio"
2404
+ return status, readme_code, language, model_url
2405
+
2406
+ else:
2407
+ # No code found
2408
+ status = f"⚠️ No inference provider or transformers/diffusers code found for `{model_id}`"
2409
+ return status, "", "python", model_url
2410
+
2411
+
2412
+ def switch_model_code_type(model_id: str, current_code: str) -> Tuple[str, str]:
2413
+ """
2414
+ Switch between inference provider code and local transformers/diffusers code.
2415
+
2416
+ Args:
2417
+ model_id: The model ID
2418
+ current_code: The currently displayed code
2419
+
2420
+ Returns: (status_message, new_code)
2421
+ """
2422
+ alternatives = get_model_code_alternatives(model_id)
2423
+ inference_code = alternatives['inference']
2424
+ local_code = alternatives['local']
2425
+
2426
+ if not inference_code and not local_code:
2427
+ return "⚠️ No alternative code available for this model.", current_code
2428
+
2429
+ # Determine which code is currently shown
2430
+ is_showing_inference = current_code == inference_code
2431
+
2432
+ if is_showing_inference and local_code:
2433
+ # Switch to local code
2434
+ status = f"βœ… Switched to **Local Transformers/Diffusers Code** for `{model_id}` πŸ’»\n\nThis code runs locally on your hardware."
2435
+ return status, local_code
2436
+ elif not is_showing_inference and inference_code:
2437
+ # Switch to inference provider code
2438
+ status = f"βœ… Switched to **Inference Provider Code** for `{model_id}` ⚑\n\nThis code uses serverless HuggingFace Inference API."
2439
+ return status, inference_code
2440
+ else:
2441
+ return "⚠️ Alternative code type not available for this model.", current_code
2442
 
2443
 
2444
  def import_repo_to_app(url: str, framework: str = "Gradio") -> Tuple[str, str, str]:
anycoder_app/ui.py CHANGED
@@ -45,7 +45,8 @@ from .deploy import (
45
  _parse_repo_or_model_url, load_project_from_url, check_hf_space_url,
46
  import_repo_to_app, extract_import_statements,
47
  generate_requirements_txt_with_llm, prettify_comfyui_json_for_html,
48
- get_trending_models, import_model_from_hf, get_trending_spaces, import_space_from_hf
 
49
  )
50
  from .agent import (
51
  agent_generate_with_questions, agent_process_answers_and_generate
@@ -113,6 +114,7 @@ with gr.Blocks(
113
  models_first_change = gr.State(True)
114
  spaces_first_change = gr.State(True)
115
  agent_mode_enabled = gr.State(False)
 
116
  agent_conversation_state = gr.State({
117
  "stage": "initial", # initial, waiting_for_answers, generating
118
  "original_query": "",
@@ -155,6 +157,7 @@ with gr.Blocks(
155
  visible=True
156
  )
157
  trending_models_status = gr.Markdown(visible=False)
 
158
 
159
  # Trending HuggingFace Spaces section
160
  trending_spaces_dropdown = gr.Dropdown(
@@ -1934,7 +1937,9 @@ CMD ["streamlit", "run", "streamlit_app.py", "--server.port=7860", "--server.add
1934
  hist, # history
1935
  history_to_chatbot_messages(hist), # history_output
1936
  history_to_chatbot_messages(hist), # chat_history
1937
- False # Set first_change to False after first trigger
 
 
1938
  ]
1939
 
1940
  if not model_id or model_id == "":
@@ -1945,7 +1950,9 @@ CMD ["streamlit", "run", "streamlit_app.py", "--server.port=7860", "--server.add
1945
  hist, # history
1946
  history_to_chatbot_messages(hist), # history_output
1947
  history_to_chatbot_messages(hist), # chat_history
1948
- False # Keep first_change as False
 
 
1949
  ]
1950
 
1951
  # Import the model
@@ -1957,6 +1964,9 @@ CMD ["streamlit", "run", "streamlit_app.py", "--server.port=7860", "--server.add
1957
  # Determine code language for display
1958
  code_lang = "python"
1959
 
 
 
 
1960
  return [
1961
  gr.update(value=status, visible=True), # status
1962
  gr.update(value=code, language=code_lang), # code_output
@@ -1964,7 +1974,9 @@ CMD ["streamlit", "run", "streamlit_app.py", "--server.port=7860", "--server.add
1964
  loaded_history, # history
1965
  history_to_chatbot_messages(loaded_history), # history_output
1966
  history_to_chatbot_messages(loaded_history), # chat_history
1967
- False # Keep first_change as False
 
 
1968
  ]
1969
 
1970
  trending_models_dropdown.change(
@@ -1977,7 +1989,46 @@ CMD ["streamlit", "run", "streamlit_app.py", "--server.port=7860", "--server.add
1977
  history,
1978
  history_output,
1979
  chat_history,
1980
- models_first_change
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1981
  ]
1982
  )
1983
 
 
45
  _parse_repo_or_model_url, load_project_from_url, check_hf_space_url,
46
  import_repo_to_app, extract_import_statements,
47
  generate_requirements_txt_with_llm, prettify_comfyui_json_for_html,
48
+ get_trending_models, import_model_from_hf, get_trending_spaces, import_space_from_hf,
49
+ switch_model_code_type
50
  )
51
  from .agent import (
52
  agent_generate_with_questions, agent_process_answers_and_generate
 
114
  models_first_change = gr.State(True)
115
  spaces_first_change = gr.State(True)
116
  agent_mode_enabled = gr.State(False)
117
+ current_trending_model_id = gr.State("") # Track current trending model for code switching
118
  agent_conversation_state = gr.State({
119
  "stage": "initial", # initial, waiting_for_answers, generating
120
  "original_query": "",
 
157
  visible=True
158
  )
159
  trending_models_status = gr.Markdown(visible=False)
160
+ switch_model_code_btn = gr.Button("πŸ”„ Switch Code Type", visible=False, size="sm", variant="secondary")
161
 
162
  # Trending HuggingFace Spaces section
163
  trending_spaces_dropdown = gr.Dropdown(
 
1937
  hist, # history
1938
  history_to_chatbot_messages(hist), # history_output
1939
  history_to_chatbot_messages(hist), # chat_history
1940
+ False, # Set first_change to False after first trigger
1941
+ gr.update(visible=False), # switch_model_code_btn
1942
+ "" # current_trending_model_id
1943
  ]
1944
 
1945
  if not model_id or model_id == "":
 
1950
  hist, # history
1951
  history_to_chatbot_messages(hist), # history_output
1952
  history_to_chatbot_messages(hist), # chat_history
1953
+ False, # Keep first_change as False
1954
+ gr.update(visible=False), # switch_model_code_btn
1955
+ "" # current_trending_model_id
1956
  ]
1957
 
1958
  # Import the model
 
1964
  # Determine code language for display
1965
  code_lang = "python"
1966
 
1967
+ # Check if button should be visible (both code types available)
1968
+ show_switch_btn = "Found multiple code options" in status
1969
+
1970
  return [
1971
  gr.update(value=status, visible=True), # status
1972
  gr.update(value=code, language=code_lang), # code_output
 
1974
  loaded_history, # history
1975
  history_to_chatbot_messages(loaded_history), # history_output
1976
  history_to_chatbot_messages(loaded_history), # chat_history
1977
+ False, # Keep first_change as False
1978
+ gr.update(visible=show_switch_btn), # switch_model_code_btn
1979
+ model_id # current_trending_model_id
1980
  ]
1981
 
1982
  trending_models_dropdown.change(
 
1989
  history,
1990
  history_output,
1991
  chat_history,
1992
+ models_first_change,
1993
+ switch_model_code_btn,
1994
+ current_trending_model_id
1995
+ ]
1996
+ )
1997
+
1998
+ # Handle switching between inference provider and local code
1999
+ def handle_switch_model_code(model_id, current_code, hist):
2000
+ """Switch between inference provider and local transformers/diffusers code"""
2001
+ if not model_id:
2002
+ return [
2003
+ gr.update(), # status
2004
+ gr.update(), # code_output
2005
+ hist, # history
2006
+ history_to_chatbot_messages(hist), # history_output
2007
+ history_to_chatbot_messages(hist) # chat_history
2008
+ ]
2009
+
2010
+ status_msg, new_code = switch_model_code_type(model_id, current_code)
2011
+
2012
+ # Update history with switch message
2013
+ switch_history = hist + [[f"Switched code type for {model_id}", new_code]]
2014
+
2015
+ return [
2016
+ gr.update(value=status_msg, visible=True), # status
2017
+ gr.update(value=new_code, language="python"), # code_output
2018
+ switch_history, # history
2019
+ history_to_chatbot_messages(switch_history), # history_output
2020
+ history_to_chatbot_messages(switch_history) # chat_history
2021
+ ]
2022
+
2023
+ switch_model_code_btn.click(
2024
+ handle_switch_model_code,
2025
+ inputs=[current_trending_model_id, code_output, history],
2026
+ outputs=[
2027
+ trending_models_status,
2028
+ code_output,
2029
+ history,
2030
+ history_output,
2031
+ chat_history
2032
  ]
2033
  )
2034