Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import requests
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# Retrieve secrets from the Hugging Face environment
|
| 6 |
+
ACCOUNT_ID = os.environ.get("CLOUDFLARE_ACCOUNT_ID")
|
| 7 |
+
API_TOKEN = os.environ.get("CLOUDFLARE_API_TOKEN")
|
| 8 |
+
|
| 9 |
+
# We will use Llama-3 as the initial reasoning engine for testing
|
| 10 |
+
MODEL = "@cf/meta/llama-3.1-8b-instruct"
|
| 11 |
+
|
| 12 |
+
def ikm_reasoning_test(user_input):
|
| 13 |
+
if not ACCOUNT_ID or not API_TOKEN:
|
| 14 |
+
return "System Error: Cloudflare credentials not found in environment variables."
|
| 15 |
+
|
| 16 |
+
url = f"https://api.cloudflare.com/client/v4/accounts/{ACCOUNT_ID}/ai/run/{MODEL}"
|
| 17 |
+
headers = {
|
| 18 |
+
"Authorization": f"Bearer {API_TOKEN}",
|
| 19 |
+
"Content-Type": "application/json"
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
# This payload is simple now, but will eventually be fed by the Knowledge Aggregator
|
| 23 |
+
payload = {
|
| 24 |
+
"prompt": user_input
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
try:
|
| 28 |
+
response = requests.post(url, headers=headers, json=payload)
|
| 29 |
+
response.raise_for_status()
|
| 30 |
+
data = response.json()
|
| 31 |
+
return data["result"]["response"]
|
| 32 |
+
except Exception as e:
|
| 33 |
+
return f"API Communication Error: {str(e)}"
|
| 34 |
+
|
| 35 |
+
# User Interaction System - Phase 1 Interface
|
| 36 |
+
demo = gr.Interface(
|
| 37 |
+
fn=ikm_reasoning_test,
|
| 38 |
+
inputs=gr.Textbox(lines=4, placeholder="Enter a command to test the IKM Reasoning Engine..."),
|
| 39 |
+
outputs=gr.Textbox(label="IKM Response"),
|
| 40 |
+
title="Emalawi19 Intelligent Knowledge Model (IKM)",
|
| 41 |
+
description="Phase 1: Establishing the core AI connection and basic User Interaction System.",
|
| 42 |
+
theme=gr.themes.Soft()
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
if __name__ == "__main__":
|
| 46 |
+
demo.launch()
|