Spaces:
Runtime error
Runtime error
| #!/usr/bin/env python3 | |
| """ | |
| Simple client script to test the AI Text Humanizer API | |
| """ | |
| import requests | |
| import json | |
| import time | |
| # Configuration | |
| API_BASE_URL = "http://localhost:8000" | |
| def test_api_connection(): | |
| """Test if the API server is running""" | |
| try: | |
| response = requests.get(f"{API_BASE_URL}/health", timeout=5) | |
| if response.status_code == 200: | |
| print("β API server is running!") | |
| return True | |
| else: | |
| print(f"β API server responded with status {response.status_code}") | |
| return False | |
| except requests.exceptions.RequestException as e: | |
| print(f"β Cannot connect to API server: {e}") | |
| print("π‘ Make sure to run: python fastapi_server.py") | |
| return False | |
| def humanize_single_text(text, style="natural", intensity=0.7): | |
| """Humanize a single piece of text""" | |
| try: | |
| payload = { | |
| "text": text, | |
| "style": style, | |
| "intensity": intensity | |
| } | |
| response = requests.post( | |
| f"{API_BASE_URL}/humanize", | |
| json=payload, | |
| headers={"Content-Type": "application/json"} | |
| ) | |
| if response.status_code == 200: | |
| return response.json() | |
| else: | |
| print(f"β API Error: {response.status_code}") | |
| print(response.text) | |
| return None | |
| except requests.exceptions.RequestException as e: | |
| print(f"β Request failed: {e}") | |
| return None | |
| def humanize_batch_texts(texts, style="natural", intensity=0.7): | |
| """Humanize multiple texts in batch""" | |
| try: | |
| payload = { | |
| "texts": texts, | |
| "style": style, | |
| "intensity": intensity | |
| } | |
| response = requests.post( | |
| f"{API_BASE_URL}/batch_humanize", | |
| json=payload, | |
| headers={"Content-Type": "application/json"} | |
| ) | |
| if response.status_code == 200: | |
| return response.json() | |
| else: | |
| print(f"β API Error: {response.status_code}") | |
| print(response.text) | |
| return None | |
| except requests.exceptions.RequestException as e: | |
| print(f"β Request failed: {e}") | |
| return None | |
| def display_result(result): | |
| """Display humanization result in a formatted way""" | |
| if not result: | |
| return | |
| print("\n" + "="*60) | |
| print("π ORIGINAL TEXT:") | |
| print("-" * 40) | |
| print(result['original_text']) | |
| print("\n⨠HUMANIZED TEXT:") | |
| print("-" * 40) | |
| print(result['humanized_text']) | |
| print(f"\nπ STATS:") | |
| print(f" β’ Similarity Score: {result['similarity_score']:.3f}") | |
| print(f" β’ Processing Time: {result['processing_time_ms']:.1f}ms") | |
| print(f" β’ Style: {result['style'].title()}") | |
| print(f" β’ Intensity: {result['intensity']}") | |
| if result['changes_made']: | |
| print(f"\nπ CHANGES MADE:") | |
| for change in result['changes_made']: | |
| print(f" β’ {change}") | |
| else: | |
| print(f"\nπ CHANGES MADE: None") | |
| def interactive_mode(): | |
| """Interactive mode for testing""" | |
| print("\nπ― Interactive Mode") | |
| print("Type 'quit' to exit\n") | |
| while True: | |
| text = input("π Enter text to humanize: ").strip() | |
| if text.lower() in ['quit', 'exit', 'q']: | |
| print("π Goodbye!") | |
| break | |
| if not text: | |
| print("β οΈ Please enter some text.") | |
| continue | |
| # Get style preference | |
| print("\nπ¨ Choose style:") | |
| print("1. Natural") | |
| print("2. Casual") | |
| print("3. Conversational") | |
| style_choice = input("Enter choice (1-3) or press Enter for Natural: ").strip() | |
| style_map = {'1': 'natural', '2': 'casual', '3': 'conversational'} | |
| style = style_map.get(style_choice, 'natural') | |
| # Get intensity | |
| intensity_input = input("β‘ Enter intensity (0.1-1.0) or press Enter for 0.7: ").strip() | |
| try: | |
| intensity = float(intensity_input) if intensity_input else 0.7 | |
| intensity = max(0.1, min(1.0, intensity)) # Clamp between 0.1 and 1.0 | |
| except ValueError: | |
| intensity = 0.7 | |
| print(f"\nπ Processing with {style} style, intensity {intensity}...") | |
| result = humanize_single_text(text, style, intensity) | |
| display_result(result) | |
| print("\n" + "-"*60 + "\n") | |
| def run_examples(): | |
| """Run example demonstrations""" | |
| print("\nπ― Running Example Tests") | |
| print("=" * 50) | |
| examples = [ | |
| { | |
| "text": "Furthermore, it is important to note that artificial intelligence systems demonstrate significant capabilities in natural language processing tasks. Subsequently, these systems can analyze and generate text with remarkable accuracy.", | |
| "style": "conversational", | |
| "intensity": 0.8, | |
| "description": "AI-formal text β Conversational" | |
| }, | |
| { | |
| "text": "The implementation of this comprehensive solution will facilitate the optimization of business processes and operational workflows. Moreover, it will demonstrate substantial improvements in efficiency metrics.", | |
| "style": "natural", | |
| "intensity": 0.6, | |
| "description": "Business text β Natural" | |
| }, | |
| { | |
| "text": "In conclusion, the systematic analysis reveals that the proposed methodology demonstrates significant potential for enhancing performance indicators.", | |
| "style": "casual", | |
| "intensity": 0.7, | |
| "description": "Academic text β Casual" | |
| } | |
| ] | |
| for i, example in enumerate(examples, 1): | |
| print(f"\n㪠Example {i}: {example['description']}") | |
| print("-" * 50) | |
| result = humanize_single_text( | |
| text=example['text'], | |
| style=example['style'], | |
| intensity=example['intensity'] | |
| ) | |
| display_result(result) | |
| # Small delay between examples | |
| time.sleep(1) | |
| def test_batch_processing(): | |
| """Test batch processing functionality""" | |
| print("\nπ Testing Batch Processing") | |
| print("=" * 50) | |
| texts = [ | |
| "Furthermore, the comprehensive analysis demonstrates significant improvements.", | |
| "Subsequently, the implementation will facilitate optimization of processes.", | |
| "Therefore, it is essential to utilize these methodologies effectively." | |
| ] | |
| print(f"π¦ Processing {len(texts)} texts in batch...") | |
| start_time = time.time() | |
| result = humanize_batch_texts(texts, style="casual", intensity=0.7) | |
| total_time = time.time() - start_time | |
| if result: | |
| print(f"\nβ Batch processing completed in {total_time:.1f}s") | |
| print(f"β‘ Total API time: {result['total_processing_time_ms']:.1f}ms") | |
| for i, text_result in enumerate(result['results'], 1): | |
| print(f"\nπ Text {i}:") | |
| print(f" Original: {text_result['original_text'][:50]}...") | |
| print(f" Humanized: {text_result['humanized_text'][:50]}...") | |
| print(f" Similarity: {text_result['similarity_score']:.3f}") | |
| def main(): | |
| """Main function""" | |
| print("π€β‘οΈπ€ AI Text Humanizer - API Client") | |
| print("=" * 50) | |
| # Test API connection | |
| if not test_api_connection(): | |
| return | |
| while True: | |
| print("\nπ― Choose an option:") | |
| print("1. Run example demonstrations") | |
| print("2. Test batch processing") | |
| print("3. Interactive mode") | |
| print("4. Exit") | |
| choice = input("\nEnter your choice (1-4): ").strip() | |
| if choice == '1': | |
| run_examples() | |
| elif choice == '2': | |
| test_batch_processing() | |
| elif choice == '3': | |
| interactive_mode() | |
| elif choice == '4': | |
| print("\nπ Thanks for using AI Text Humanizer!") | |
| break | |
| else: | |
| print("β Invalid choice. Please enter 1, 2, 3, or 4.") | |
| if __name__ == "__main__": | |
| main() |