mport gradio as gr import os import requests GROK_KEY = os.getenv("GROK_KEY") GROK_URL = "https://api.grokapi.com/v1/generate" def call_grok(prompt, max_tokens=300): headers = {"Authorization": f"Bearer {GROK_KEY}"} payload = { "prompt": prompt, "max_tokens": max_tokens, "temperature": 0.2 } response = requests.post(GROK_URL, headers=headers, json=payload) if response.status_code == 200: return response.json()["text"] else: return f"Error: {response.status_code} - {response.text}" def css_lexiscan_final(text_input): if not text_input.strip(): return "⚠️ Please enter some text.", "⚠️ Please enter some text." import re precis_prompt = f""" Summarize the text below following PASI rules (CSS exam): - One-third length - Suitable title - Third-person pronouns - No personal opinions/examples - Condensed meaning Output format: <<>> <Title here> <<<PRECIS>>> <Precis here> Text: {text_input} """ precis_raw = call_grok(precis_prompt, max_tokens=300) title_match = re.search(r'<<<TITLE>>>\s*(.?)\s<<<PRECIS>>>', precis_raw, re.DOTALL) precis_match = re.search(r'<<<PRECIS>>>\s*(.*)', precis_raw, re.DOTALL) title_text = title_match.group(1).strip() if title_match else "" precis_text = precis_match.group(1).strip() if precis_match else "" final_precis = f"{title_text}\n{precis_text}" vocab_prompt = f""" Extract 5-7 difficult or uncommon words from the following text and give their meanings in simple English. Use numbered list format: 1. example_word: meaning 2. another_word: meaning 3. yet_another_word: meaning Only include words actually present in the text. Output ONLY the list. Text: {text_input} """ vocab_raw = call_grok(vocab_prompt, max_tokens=250) vocab_clean = vocab_raw.replace(text_input, "").strip() return final_precis, vocab_clean # ----------------------------- GRADIO UI ----------------------------- custom_css = """ <style> body {background-color: #1E1E2F; color: #FFFFFF; font-family: 'Poppins', sans-serif;} .gradio-container {border-radius: 20px; padding: 20px;} .gr-button {background-color: #FF6B6B !important; color: white !important; font-weight: bold;} textarea {font-size: 18px !important; min-height: 180px !important;} label {font-size: 20px !important; font-weight: bold !important;} h1 {color: #FFD93D;} h2 {color: #6BCB77;} </style> """ with gr.Blocks() as demo: gr.HTML(custom_css) gr.Markdown("<h1>✨ CSS LexiScan MVP ✨</h1>") gr.Markdown("<h2>Enter text & get PASI-style precis + vocabulary</h2>") with gr.Row(): with gr.Column(): text_input = gr.Textbox(label="Enter Editorial / Paragraph", placeholder="Paste your text here...", lines=8) submit_btn = gr.Button("Analyze Text") with gr.Column(): precis_output = gr.Textbox(label="📜 Precis Output", lines=10) vocab_output = gr.Textbox(label="📝 Difficult Words + Meanings", lines=10) submit_btn.click(css_lexiscan_final, inputs=text_input, outputs=[precis_output, vocab_output]) demo.launch()