Spaces:
Configuration error
Configuration error
import os | |
import requests | |
from flask import Flask, request, jsonify | |
app = Flask(__name__) | |
# The HTML and CSS for the mobile-friendly user interface. | |
# The design is modern, interactive, and uses Tailwind CSS. | |
# You can modify the design from here. | |
# (هنا تبدأ الواجهة الأمامية للتطبيق، وهي مصممة بأسلوب عصري ومناسب للجوالات باستخدام Tailwind CSS) | |
html_template = """ | |
<!DOCTYPE html> | |
<html lang="ar"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Worm Chat GPT</title> | |
<!-- Tailwind CSS CDN for styling --> | |
<script src="https://cdn.tailwindcss.com"></script> | |
<style> | |
@import url('https://fonts.googleapis.com/css2?family=Tajawal:wght@400;700&display=swap'); | |
body { | |
font-family: 'Tajawal', sans-serif; | |
direction: rtl; /* Set text direction to right-to-left for Arabic */ | |
} | |
</style> | |
</head> | |
<body class="bg-gradient-to-br from-indigo-900 via-purple-900 to-pink-900 text-white min-h-screen flex items-center justify-center p-4"> | |
<div class="bg-white/10 backdrop-blur-md p-6 sm:p-8 rounded-3xl shadow-2xl w-full max-w-sm flex flex-col items-center border border-white/20"> | |
<!-- Application Title --> | |
<h1 class="text-3xl font-bold mb-2">Worm Chat GPT</h1> | |
<p class="text-sm text-center text-white/70 mb-6">أهلاً بك، أرسل طلبك وسيتم تنفيذه</p> | |
<!-- Input area --> | |
<div class="w-full mb-4"> | |
<textarea id="text-input" rows="4" class="w-full p-4 rounded-xl bg-white/20 text-white placeholder-white/50 focus:outline-none focus:ring-2 focus:ring-purple-400 transition-all resize-none" placeholder="اكتب طلبك هنا..."></textarea> | |
</div> | |
<!-- Submit button --> | |
<button id="send-btn" class="w-full bg-indigo-500 hover:bg-indigo-600 transition-all text-white font-bold py-3 px-6 rounded-full shadow-lg transform hover:scale-105 active:scale-95 disabled:bg-indigo-300 disabled:cursor-not-allowed"> | |
إرسال | |
</button> | |
<!-- Loading spinner and response area --> | |
<div id="loading" class="mt-6 hidden"> | |
<div class="animate-spin rounded-full h-8 w-8 border-t-2 border-b-2 border-purple-500"></div> | |
</div> | |
<div id="response-container" class="w-full mt-6 p-4 rounded-xl bg-white/10 border border-white/20 hidden"> | |
<p id="response-text" class="text-sm leading-relaxed"></p> | |
</div> | |
</div> | |
<!-- JavaScript for handling user interactions and API calls --> | |
<script> | |
// Get references to HTML elements | |
const textInput = document.getElementById('text-input'); | |
const sendBtn = document.getElementById('send-btn'); | |
const loadingSpinner = document.getElementById('loading'); | |
const responseContainer = document.getElementById('response-container'); | |
const responseText = document.getElementById('response-text'); | |
// Event listener for the send button | |
sendBtn.addEventListener('click', async () => { | |
const text = textInput.value.trim(); | |
if (!text) { | |
alert('الرجاء كتابة طلبك أولاً.'); | |
return; | |
} | |
// Show loading spinner and disable button | |
sendBtn.disabled = true; | |
loadingSpinner.classList.remove('hidden'); | |
responseContainer.classList.add('hidden'); | |
responseText.textContent = ''; | |
try { | |
// Send the request to our backend | |
const response = await fetch('/api/process', { | |
method: 'POST', | |
headers: { 'Content-Type': 'application/json' }, | |
body: JSON.stringify({ text: text }) | |
}); | |
if (!response.ok) { | |
throw new Error('فشل الاتصال بالخادم.'); | |
} | |
const data = await response.json(); | |
// Display the result in the response container | |
responseText.textContent = data.result || 'لم يتم العثور على نتيجة.'; | |
responseContainer.classList.remove('hidden'); | |
} catch (error) { | |
// Display error message | |
responseText.textContent = 'حدث خطأ: ' + error.message; | |
responseContainer.classList.remove('hidden'); | |
} finally { | |
// Hide loading spinner and enable the button | |
loadingSpinner.classList.add('hidden'); | |
sendBtn.disabled = false; | |
} | |
}); | |
</script> | |
</body> | |
</html> | |
""" | |
# The HTML and CSS for the mobile-friendly user interface ends here. | |
def index(): | |
"""Renders the main HTML page for the web application.""" | |
return html_template | |
def process_text(): | |
"""Handles text processing requests.""" | |
try: | |
# Get the text from the request body | |
data = request.get_json() | |
user_text = data.get('text', '').strip() | |
if not user_text: | |
return jsonify({"error": "الرجاء إرسال نص صالح."}), 400 | |
# Construct the URL for the external API | |
api_url = f'https://dev-pycodz-blackbox.pantheonsite.io/DEvZ44d/DarkCode.php?text={user_text}' | |
# Make the GET request to the external API | |
response = requests.get(api_url) | |
response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx) | |
# The external API response is the text we need | |
processed_text = response.text | |
# Return the processed text in a JSON format | |
return jsonify({"result": processed_text}) | |
except requests.exceptions.RequestException as e: | |
# Handle network or HTTP errors when connecting to the external API | |
return jsonify({"error": f"حدث خطأ في الاتصال بالخدمة الخارجية: {str(e)}"}), 500 | |
except Exception as e: | |
# Handle any other unexpected errors | |
return jsonify({"error": f"حدث خطأ غير متوقع: {str(e)}"}), 500 | |
# This block is crucial for running the application on Hugging Face Spaces | |
if __name__ == "__main__": | |
app.run(host='0.0.0.0', port=int(os.environ.get("PORT", 5000))) | |