Spaces:
Sleeping
Sleeping
| import os | |
| import json | |
| import random | |
| import time | |
| from datetime import datetime, timedelta | |
| from flask import Flask, jsonify, request, render_template, send_from_directory | |
| from flask_cors import CORS | |
| from faker import Faker | |
| from openai import OpenAI | |
| app = Flask(__name__, static_folder='static', template_folder='templates') | |
| app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16MB max upload | |
| CORS(app) | |
| fake = Faker('zh_CN') | |
| # SiliconFlow Configuration | |
| SILICON_API_KEY = "sk-vimuseiptfbomzegyuvmebjzooncsqbyjtlddrfodzcdskgi" | |
| client = OpenAI( | |
| api_key=SILICON_API_KEY, | |
| base_url="https://api.siliconflow.cn/v1" | |
| ) | |
| DATA_FILE = 'prompts.json' | |
| def load_data(): | |
| if os.path.exists(DATA_FILE): | |
| try: | |
| with open(DATA_FILE, 'r', encoding='utf-8') as f: | |
| return json.load(f) | |
| except: | |
| return [] | |
| return [] | |
| def save_data(data): | |
| try: | |
| with open(DATA_FILE, 'w', encoding='utf-8') as f: | |
| json.dump(data, f, ensure_ascii=False, indent=2) | |
| except Exception as e: | |
| print(f"Error saving data: {e}") | |
| # --- Mock Data Store --- | |
| # Initialize with persistence or defaults | |
| PROMPTS = load_data() | |
| if not PROMPTS: | |
| PROMPTS = [ | |
| { | |
| "id": "p-101", | |
| "name": "Customer Support Empathetic Reply", | |
| "description": "Handles angry customer complaints with empathy.", | |
| "version": "1.2", | |
| "tags": ["Support", "Email", "B2C"], | |
| "content": "You are a helpful support agent. A customer is angry about a late delivery. Write a polite response apologizing and offering a 10% discount.", | |
| "metrics": {"accuracy": 0.88, "latency_ms": 450, "cost": 0.002}, | |
| "last_updated": "2023-10-25T10:30:00" | |
| }, | |
| { | |
| "id": "p-102", | |
| "name": "Python Code Refactor", | |
| "description": "Refactors legacy code to modern standards.", | |
| "version": "2.0", | |
| "tags": ["Coding", "Python", "DevTools"], | |
| "content": "You are a senior Python engineer. Refactor the following code to adhere to PEP8 and improve performance. Add type hints.", | |
| "metrics": {"accuracy": 0.95, "latency_ms": 1200, "cost": 0.015}, | |
| "last_updated": "2023-10-26T14:15:00" | |
| }, | |
| { | |
| "id": "p-103", | |
| "name": "Marketing Copy Generator", | |
| "description": "Generates catchy headlines for social media.", | |
| "version": "0.9", | |
| "tags": ["Marketing", "Social", "Creative"], | |
| "content": "Write 5 catchy headlines for a new coffee brand that focuses on sustainability.", | |
| "metrics": {"accuracy": 0.76, "latency_ms": 300, "cost": 0.001}, | |
| "last_updated": "2023-10-27T09:00:00" | |
| } | |
| ] | |
| save_data(PROMPTS) | |
| # --- Routes --- | |
| def index(): | |
| return render_template('index.html') | |
| def get_prompts(): | |
| return jsonify(PROMPTS) | |
| def create_prompt(): | |
| data = request.json | |
| new_prompt = { | |
| "id": f"p-{random.randint(200, 999)}", | |
| "name": data.get("name", "New Prompt"), | |
| "description": data.get("description", ""), | |
| "version": "0.1", | |
| "tags": data.get("tags", []), | |
| "content": data.get("content", ""), | |
| "metrics": {"accuracy": 0.0, "latency_ms": 0, "cost": 0.0}, | |
| "last_updated": datetime.now().isoformat() | |
| } | |
| PROMPTS.insert(0, new_prompt) | |
| save_data(PROMPTS) | |
| return jsonify(new_prompt), 201 | |
| def update_prompt(prompt_id): | |
| data = request.json | |
| for prompt in PROMPTS: | |
| if prompt['id'] == prompt_id: | |
| prompt['content'] = data.get('content', prompt['content']) | |
| prompt['last_updated'] = datetime.now().isoformat() | |
| save_data(PROMPTS) | |
| return jsonify(prompt) | |
| return jsonify({"error": "Prompt not found"}), 404 | |
| def optimize_prompt(): | |
| """Optimizes prompt using SiliconFlow API.""" | |
| data = request.json | |
| original_content = data.get("content", "") | |
| if not original_content: | |
| return jsonify({"error": "Content is required"}), 400 | |
| try: | |
| response = client.chat.completions.create( | |
| model="Qwen/Qwen2.5-7B-Instruct", | |
| messages=[ | |
| {"role": "system", "content": "You are an expert Prompt Engineer. Optimize the user's prompt for better clarity, structure, and effectiveness. Return ONLY the optimized prompt content, nothing else."}, | |
| {"role": "user", "content": original_content} | |
| ], | |
| temperature=0.7, | |
| max_tokens=1024 | |
| ) | |
| improved_content = response.choices[0].message.content.strip() | |
| # Simple heuristic improvements list (since we only get the content back) | |
| improvements = [ | |
| "Enhanced clarity and structure", | |
| "Added specific constraints", | |
| "Improved professional tone" | |
| ] | |
| return jsonify({ | |
| "original": original_content, | |
| "optimized": improved_content, | |
| "improvements": improvements | |
| }) | |
| except Exception as e: | |
| print(f"API Error: {e}") | |
| # Fallback to mock if API fails | |
| time.sleep(1) | |
| return jsonify({ | |
| "original": original_content, | |
| "optimized": f"Optimization failed (API Error). Preserving original: {original_content}", | |
| "improvements": ["Error connecting to AI service"] | |
| }) | |
| def evaluate_prompt(): | |
| """Evaluates prompt using SiliconFlow API.""" | |
| data = request.json | |
| content = data.get("content", "") | |
| if not content: | |
| return jsonify({"error": "Content is required"}), 400 | |
| try: | |
| # Construct evaluation prompt | |
| eval_prompt = f""" | |
| Evaluate the following system prompt based on these criteria: | |
| 1. Faithfulness (0-1): Does it strictly follow instructions? | |
| 2. Relevance (0-1): Is it relevant to the user's intent? | |
| 3. Safety (0-1): Is it safe and harmless? | |
| System Prompt to Evaluate: | |
| "{content}" | |
| Return a JSON object with keys: faithfulness, relevance, safety. | |
| Example: {{"faithfulness": 0.9, "relevance": 0.85, "safety": 1.0}} | |
| """ | |
| response = client.chat.completions.create( | |
| model="Qwen/Qwen2.5-7B-Instruct", | |
| messages=[ | |
| {"role": "system", "content": "You are an AI Evaluator. Return ONLY JSON."}, | |
| {"role": "user", "content": eval_prompt} | |
| ], | |
| temperature=0.1, | |
| response_format={"type": "json_object"} | |
| ) | |
| metrics_raw = response.choices[0].message.content.strip() | |
| metrics = json.loads(metrics_raw) | |
| # Calculate overall score | |
| avg_score = (metrics.get('faithfulness', 0) + metrics.get('relevance', 0) + metrics.get('safety', 0)) / 3 | |
| return jsonify({ | |
| "overall_score": round(avg_score, 2), | |
| "metrics": metrics, | |
| "estimated_cost": 0.005, # Mock cost | |
| "latency_p95": random.randint(200, 800) | |
| }) | |
| except Exception as e: | |
| print(f"Eval Error: {e}") | |
| # Fallback to mock | |
| time.sleep(1.0) | |
| base_score = random.uniform(0.7, 0.95) | |
| return jsonify({ | |
| "overall_score": round(base_score, 2), | |
| "metrics": { | |
| "faithfulness": round(random.uniform(0.8, 1.0), 2), | |
| "relevance": round(random.uniform(0.7, 0.95), 2), | |
| "safety": round(random.uniform(0.9, 1.0), 2) | |
| }, | |
| "estimated_cost": round(random.uniform(0.001, 0.02), 4), | |
| "latency_p95": random.randint(200, 1500) | |
| }) | |
| def get_dashboard_stats(): | |
| # Mock aggregated stats | |
| dates = [(datetime.now() - timedelta(days=i)).strftime('%m-%d') for i in range(6, -1, -1)] | |
| calls = [random.randint(1000, 5000) for _ in range(7)] | |
| errors = [random.randint(0, 50) for _ in range(7)] | |
| avg_latency = [random.randint(300, 800) for _ in range(7)] | |
| return jsonify({ | |
| "total_prompts": len(PROMPTS), | |
| "total_calls_today": 3421, | |
| "avg_accuracy": 0.87, | |
| "cost_saved": "$124.50", | |
| "chart_data": { | |
| "labels": dates, | |
| "calls": calls, | |
| "errors": errors, | |
| "latency": avg_latency | |
| } | |
| }) | |
| def upload_prompts(): | |
| if 'file' not in request.files: | |
| return jsonify({"error": "No file part"}), 400 | |
| file = request.files['file'] | |
| if file.filename == '': | |
| return jsonify({"error": "No selected file"}), 400 | |
| try: | |
| if file.filename.endswith('.json'): | |
| content = json.load(file) | |
| # Basic validation | |
| if isinstance(content, list): | |
| for item in content: | |
| item['id'] = f"p-{random.randint(1000, 9999)}" | |
| item['last_updated'] = datetime.now().isoformat() | |
| # Ensure defaults | |
| if 'metrics' not in item: | |
| item['metrics'] = {"accuracy": 0.0, "latency_ms": 0, "cost": 0.0} | |
| PROMPTS.insert(0, item) | |
| return jsonify({"message": f"Successfully imported {len(content)} prompts", "count": len(content)}) | |
| else: | |
| return jsonify({"error": "Invalid JSON format. Expected a list."}), 400 | |
| else: | |
| return jsonify({"error": "Only .json files are supported"}), 400 | |
| except Exception as e: | |
| return jsonify({"error": str(e)}), 500 | |
| if __name__ == '__main__': | |
| # Use port 7860 for Hugging Face Spaces | |
| app.run(host='0.0.0.0', port=7860, debug=True) | |