Spaces:
Running

File size: 2,368 Bytes
1bf6fdb
36b5ce1
 
 
 
 
 
 
 
 
 
 
 
 
dee59ab
36b5ce1
1bf6fdb
dee59ab
 
36b5ce1
 
 
 
 
 
dee59ab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36b5ce1
dee59ab
 
 
 
 
36b5ce1
dee59ab
 
 
 
 
 
36b5ce1
 
 
dee59ab
 
36b5ce1
1bf6fdb
 
36b5ce1
 
 
 
 
1bf6fdb
 
 
36b5ce1
 
1bf6fdb
36b5ce1
1bf6fdb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Groq Summarizer - Gradio Lite</title>
    
    <!-- Load Gradio-Lite CSS and JS -->
    <script type="module" src="https://cdn.jsdelivr.net/npm/@gradio/lite/dist/lite.js"></script>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@gradio/lite/dist/lite.css" />
</head>
<body>

    <gradio-lite>
        <!-- No external requirements needed; uses standard urllib -->
        <gradio-file name="app.py">
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()
        </gradio-file>
    </gradio-lite>

</body>
</html>