Spaces:
Running
Running
final
Browse files
app.py
CHANGED
|
@@ -2,7 +2,11 @@ import gradio as gr
|
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
import os
|
| 4 |
|
|
|
|
|
|
|
| 5 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
|
|
|
|
|
|
| 6 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta", token=HF_TOKEN)
|
| 7 |
|
| 8 |
class StateController:
|
|
@@ -33,8 +37,8 @@ class StateController:
|
|
| 33 |
self.state_array = [0] * 121
|
| 34 |
return "System resolved. State array reset to zero."
|
| 35 |
|
| 36 |
-
def
|
| 37 |
-
# Hardware
|
| 38 |
if "run grid diagnostic" in message.lower():
|
| 39 |
controller = StateController()
|
| 40 |
output = "Diagnostic sequence initiated.\n\n"
|
|
@@ -43,54 +47,44 @@ def process_request(message, history):
|
|
| 43 |
output += f"`{controller.render_grid()}`\n\n"
|
| 44 |
output += "Executing state resolution:\n"
|
| 45 |
output += f"`{controller.resolve_grid()}`"
|
| 46 |
-
|
| 47 |
-
return
|
| 48 |
|
| 49 |
system_instruction = (
|
| 50 |
"You are a logic-focused inference engine. "
|
| 51 |
-
"You utilize strict state-hold memory and parallel integer blocks
|
| 52 |
-
"Provide
|
| 53 |
)
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
| 60 |
|
| 61 |
-
response_text = ""
|
| 62 |
try:
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
| 65 |
max_tokens=1024,
|
| 66 |
-
stream=
|
| 67 |
-
)
|
| 68 |
-
|
| 69 |
-
if token:
|
| 70 |
-
response_text += token
|
| 71 |
-
yield response_text
|
| 72 |
except Exception as error:
|
| 73 |
-
|
| 74 |
-
yield error_message
|
| 75 |
-
|
| 76 |
-
custom_css = """
|
| 77 |
-
body, .gradio-container { background-color: #0b0f19 !important; }
|
| 78 |
-
footer {display: none !important}
|
| 79 |
-
.message.user { background-color: #1e293b !important; border: 1px solid #3b82f6 !important; }
|
| 80 |
-
.message.bot { background-color: #0f172a !important; color: #60a5fa !important; }
|
| 81 |
-
"""
|
| 82 |
|
| 83 |
-
|
|
|
|
| 84 |
gr.Markdown("# Advanced Logic Interface")
|
| 85 |
gr.ChatInterface(
|
| 86 |
-
fn=
|
| 87 |
-
description="Inference layer utilizing
|
| 88 |
examples=[
|
| 89 |
"Run grid diagnostic",
|
| 90 |
-
"
|
| 91 |
-
"
|
| 92 |
-
]
|
| 93 |
-
cache_examples=False
|
| 94 |
)
|
| 95 |
|
| 96 |
if __name__ == "__main__":
|
|
|
|
| 2 |
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:
|
|
|
|
| 37 |
self.state_array = [0] * 121
|
| 38 |
return "System resolved. State array reset to zero."
|
| 39 |
|
| 40 |
+
def generate_response(message, history):
|
| 41 |
+
# Hardware diagnostic override
|
| 42 |
if "run grid diagnostic" in message.lower():
|
| 43 |
controller = StateController()
|
| 44 |
output = "Diagnostic sequence initiated.\n\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 = (
|
| 53 |
"You are a logic-focused inference engine. "
|
| 54 |
+
"You utilize strict state-hold memory and parallel integer blocks. "
|
| 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 |
|
| 90 |
if __name__ == "__main__":
|