Godheritage commited on
Commit
52540c7
·
verified ·
1 Parent(s): 7581139

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -29
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import gradio as gr
2
  import os
3
- from huggingface_hub import InferenceClient
4
  from get_machine_from_json import string_to_bsg
5
  import json
6
 
@@ -17,17 +17,13 @@ try:
17
  except:
18
  pass
19
 
20
- # Available models on HF Inference API (confirmed working)
21
- AVAILABLE_MODELS = [
22
- "Qwen/Qwen2.5-72B-Instruct", # Qwen 72B - Very good performance
23
- "meta-llama/Llama-3.2-3B-Instruct", # Llama 3B - Fast and reliable
24
- "mistralai/Mistral-7B-Instruct-v0.3", # Mistral 7B - Good balance
25
- "microsoft/Phi-3-mini-4k-instruct", # Phi 3 Mini - Small and fast
26
- ]
27
 
28
- # Initialize HF Inference client
29
  def create_client():
30
- """Create HF Inference Client"""
31
  # Get token from environment variable
32
  hf_token = os.environ.get("HF_TOKEN")
33
 
@@ -44,19 +40,10 @@ def create_client():
44
  "Token should have READ permission and start with 'hf_'"
45
  )
46
 
47
- # Try models in priority order
48
- for model in AVAILABLE_MODELS:
49
- try:
50
- client = InferenceClient(model=model, token=hf_token)
51
- print(f"✅ Successfully connected to model: {model}")
52
- return client
53
- except Exception as e:
54
- print(f"⚠️ Failed to connect to {model}: {e}")
55
- continue
56
-
57
- # Fallback to first model if all failed
58
- print(f"⚠️ All models failed, using fallback: {AVAILABLE_MODELS[0]}")
59
- return InferenceClient(model=AVAILABLE_MODELS[0], token=hf_token)
60
 
61
  def generate_machine(user_prompt, temperature=0.7, max_tokens=4096):
62
  """
@@ -82,17 +69,20 @@ def generate_machine(user_prompt, temperature=0.7, max_tokens=4096):
82
  {"role": "user", "content": user_prompt}
83
  ]
84
 
85
- # Call HF Inference API
86
  response = ""
87
  try:
88
- for message in client.chat_completion(
 
89
  messages=messages,
90
  temperature=temperature,
91
  max_tokens=max_tokens,
92
  stream=True,
93
- ):
94
- if message.choices[0].delta.content:
95
- response += message.choices[0].delta.content
 
 
96
 
97
  except Exception as api_error:
98
  error_msg = str(api_error)
@@ -518,7 +508,7 @@ with gr.Blocks(css=custom_css, theme=gr.themes.Soft(), title="🎮 Besiege Machi
518
  gr.HTML(f"""
519
  <div class="footer-box">
520
  <p>
521
- 🤖 Using <a href="https://huggingface.co/{AVAILABLE_MODELS[0]}" target="_blank">{AVAILABLE_MODELS[0]}</a> (HF Inference API)
522
  </p>
523
  <p>
524
  ⚠️ Note: Generated machines may require adjustments in-game
 
1
  import gradio as gr
2
  import os
3
+ from openai import OpenAI
4
  from get_machine_from_json import string_to_bsg
5
  import json
6
 
 
17
  except:
18
  pass
19
 
20
+ # Model configuration - Using HF Router with DeepSeek
21
+ MODEL_NAME = "deepseek-ai/DeepSeek-V3.2-Exp"
22
+ HF_ROUTER_BASE_URL = "https://router.huggingface.co/v1"
 
 
 
 
23
 
24
+ # Initialize OpenAI-compatible client for HF Router
25
  def create_client():
26
+ """Create OpenAI-compatible client using HF Router"""
27
  # Get token from environment variable
28
  hf_token = os.environ.get("HF_TOKEN")
29
 
 
40
  "Token should have READ permission and start with 'hf_'"
41
  )
42
 
43
+ return OpenAI(
44
+ base_url=HF_ROUTER_BASE_URL,
45
+ api_key=hf_token
46
+ )
 
 
 
 
 
 
 
 
 
47
 
48
  def generate_machine(user_prompt, temperature=0.7, max_tokens=4096):
49
  """
 
69
  {"role": "user", "content": user_prompt}
70
  ]
71
 
72
+ # Call HF Router API
73
  response = ""
74
  try:
75
+ stream = client.chat.completions.create(
76
+ model=MODEL_NAME,
77
  messages=messages,
78
  temperature=temperature,
79
  max_tokens=max_tokens,
80
  stream=True,
81
+ )
82
+
83
+ for chunk in stream:
84
+ if chunk.choices[0].delta.content:
85
+ response += chunk.choices[0].delta.content
86
 
87
  except Exception as api_error:
88
  error_msg = str(api_error)
 
508
  gr.HTML(f"""
509
  <div class="footer-box">
510
  <p>
511
+ 🤖 Using <a href="https://huggingface.co/{MODEL_NAME}" target="_blank">{MODEL_NAME}</a> (HF Router)
512
  </p>
513
  <p>
514
  ⚠️ Note: Generated machines may require adjustments in-game