import gradio as gr import urllib.request import json def summarize_text(api_key, text): if not api_key: return "Please provide an API key." if not text: return "Please provide text to summarize." url = "https://api.groq.com/openai/v1/chat/completions" headers = { "Authorization": f"Bearer {api_key.strip()}", "Content-Type": "application/json" } payload = { "model": "llama-3.1-8b-instant", "messages": [ {"role": "system", "content": "You are a helpful assistant that summarizes text clearly and concisely."}, {"role": "user", "content": f"Summarize the following text:\n\n{text}"} ] } try: req = urllib.request.Request( url, data=json.dumps(payload).encode("utf-8"), headers=headers, method="POST" ) with urllib.request.urlopen(req) as response: res_data = json.loads(response.read().decode("utf-8")) return res_data["choices"][0]["message"]["content"] except urllib.error.HTTPError as e: error_body = e.read().decode("utf-8") return f"HTTP Error {e.code}: {error_body}" except Exception as e: return f"An error occurred: {str(e)}" api_key = gr.Textbox(label="Groq API Key", type="password", placeholder="gsk_...") text = gr.Textbox(label="Text to summarize", lines=6, placeholder="Paste text here...") output_text = gr.Textbox(label="Summary") demo = gr.Interface( title="Summarize a text!", description="Simple app running completely in your browser via Gradio-Lite.", fn=summarize_text, inputs=[api_key, text], outputs=[output_text] ) demo.launch()