spanofzero commited on
Commit
60330ec
·
verified ·
1 Parent(s): 51fde0f
Files changed (1) hide show
  1. app.py +27 -23
app.py CHANGED
@@ -3,11 +3,10 @@ from huggingface_hub import InferenceClient
3
  import os
4
 
5
  # Securely retrieve the token from your Space secrets
6
- # Ensure you have a secret named HF_TOKEN in your Settings
7
  HF_TOKEN = os.getenv("HF_TOKEN")
8
 
9
- # Initialize the inference client with the specified model
10
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", token=HF_TOKEN)
11
 
12
  class StateController:
13
  def __init__(self):
@@ -44,9 +43,9 @@ def generate_response(message, history):
44
  output = "Diagnostic sequence initiated.\n\n"
45
  output += f"{controller.initialize_grid()}\n\n"
46
  output += "Rendering 121-point array:\n"
47
- output += f"`{controller.render_grid()}`\n\n"
48
  output += "Executing state resolution:\n"
49
- output += f"`{controller.resolve_grid()}`"
50
  return output
51
 
52
  system_instruction = (
@@ -55,35 +54,40 @@ def generate_response(message, history):
55
  "Provide direct, technical, and accurate responses."
56
  )
57
 
58
- # Formatting for Gradio 6.5+ message history
59
- formatted_messages = [{"role": "system", "content": system_instruction}]
60
- for turn in history:
61
- formatted_messages.append({"role": "user", "content": turn[0]})
62
- formatted_messages.append({"role": "assistant", "content": turn[1]})
63
- formatted_messages.append({"role": "user", "content": message})
64
 
65
  try:
66
- response_text = ""
67
- # Direct call for response generation
68
- completion = client.chat_completion(
69
- formatted_messages,
70
- max_tokens=1024,
71
- stream=False # Set to False for maximum stability during testing
72
  )
73
- return completion.choices[0].message.content
74
  except Exception as error:
75
- return f"System Error: {str(error)}. Ensure HF_TOKEN is correctly set in Secrets."
76
 
77
- # Professional UI implementation
78
- with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
 
 
 
 
 
 
79
  gr.Markdown("# Advanced Logic Interface")
80
  gr.ChatInterface(
81
  fn=generate_response,
82
  description="Inference layer utilizing state-hold logic.",
83
  examples=[
84
  "Run grid diagnostic",
85
- "Explain network latency without using the word delay.",
86
- "Calculate allocation for 120 units across 3 nodes."
87
  ]
88
  )
89
 
 
3
  import os
4
 
5
  # Securely retrieve the token from your Space secrets
 
6
  HF_TOKEN = os.getenv("HF_TOKEN")
7
 
8
+ # Initializing with a model that has high serverless availability
9
+ client = InferenceClient("Qwen/Qwen2.5-7B-Instruct", token=HF_TOKEN)
10
 
11
  class StateController:
12
  def __init__(self):
 
43
  output = "Diagnostic sequence initiated.\n\n"
44
  output += f"{controller.initialize_grid()}\n\n"
45
  output += "Rendering 121-point array:\n"
46
+ output += f"{controller.render_grid()}\n\n"
47
  output += "Executing state resolution:\n"
48
+ output += f"{controller.resolve_grid()}"
49
  return output
50
 
51
  system_instruction = (
 
54
  "Provide direct, technical, and accurate responses."
55
  )
56
 
57
+ # Building the prompt for the text generation API
58
+ prompt = f"<|im_start|>system\n{system_instruction}<|im_end|>\n"
59
+ for user_msg, assistant_msg in history:
60
+ prompt += f"<|im_start|>user\n{user_msg}<|im_end|>\n<|im_start|>assistant\n{assistant_msg}<|im_end|>\n"
61
+ prompt += f"<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n"
 
62
 
63
  try:
64
+ # Using text_generation for direct inference API access
65
+ response = client.text_generation(
66
+ prompt,
67
+ max_new_tokens=1024,
68
+ stop_sequences=["<|im_end|>", "<|endoftext|>"],
69
+ temperature=0.1 # Low temperature for high precision
70
  )
71
+ return response.strip()
72
  except Exception as error:
73
+ return f"System Error: {str(error)}. If the error persists, ensure your HF_TOKEN has Inference permissions."
74
 
75
+ custom_css = """
76
+ body, .gradio-container { background-color: #0b0f19 !important; }
77
+ footer {display: none !important}
78
+ .message.user { background-color: #1e293b !important; border: 1px solid #3b82f6 !important; }
79
+ .message.bot { background-color: #0f172a !important; color: #60a5fa !important; }
80
+ """
81
+
82
+ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue"), css=custom_css) as demo:
83
  gr.Markdown("# Advanced Logic Interface")
84
  gr.ChatInterface(
85
  fn=generate_response,
86
  description="Inference layer utilizing state-hold logic.",
87
  examples=[
88
  "Run grid diagnostic",
89
+ "Calculate the integer distribution for 120 units across 3 nodes.",
90
+ "Explain network latency using technical terminology."
91
  ]
92
  )
93