Spaces:
Sleeping
Sleeping
import os | |
import json | |
import requests | |
import gradio as gr | |
# Load tool prompts | |
with open("system_prompts.json", "r", encoding="utf-8") as f: | |
SYSTEM_PROMPTS = json.load(f) | |
TOOLS = list(SYSTEM_PROMPTS.keys()) | |
# These are your HF Inference Endpoint settings | |
HF_API_URL = os.environ["HF_API_URL"] | |
HF_API_TOKEN = "https://api-inference.huggingface.co/models/mistralai/Mistral-7B-Instruct-v0.2" | |
def run_mistral(prompt: str, text: str) -> str: | |
""" | |
Calls the Hugging Face Inference Endpoint for Mistral using REST. | |
Sends 'prompt' as system message and 'text' as user message. | |
For reference, HF Inference endpoints accept JSON payloads like this :contentReference[oaicite:1]{index=1}. | |
""" | |
headers = { | |
"Authorization": f"Bearer {HF_API_TOKEN}", | |
"Content-Type": "application/json" | |
} | |
payload = { | |
"inputs": [ | |
{"role": "system", "content": prompt}, | |
{"role": "user", "content": text} | |
], | |
"parameters": { | |
"max_new_tokens": 512, | |
"temperature": 0.3, | |
"return_full_text": False | |
} | |
} | |
resp = requests.post(HF_API_URL, headers=headers, json=payload, timeout=60) | |
resp.raise_for_status() | |
out = resp.json() | |
# HF returns a list for chat models | |
if isinstance(out, list) and "generated_text" in out[0]: | |
return out[0]["generated_text"] | |
# else a dict | |
return out.get("generated_text", json.dumps(out)) | |
def ecoling_tool(tool: str, text: str) -> str: | |
# Looks up the prompt and calls the endpoint | |
return run_mistral(SYSTEM_PROMPTS[tool], text) | |
demo = gr.Interface( | |
fn=ecoling_tool, | |
inputs=[gr.Dropdown(TOOLS, label="Tool"), gr.Textbox(lines=8, label="Input Text")], | |
outputs=[gr.Textbox(label="Response")], | |
title="π± Ecolinguistic MCP Server", | |
description="Accessible via UI and standard MCP; powered by a HF Mistral endpoint." | |
) | |
if __name__ == "__main__": | |
demo.launch(mcp_server=True) | |