dindizz commited on
Commit
280b825
·
verified ·
1 Parent(s): b55dce3

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -0
app.py ADDED
@@ -0,0 +1,78 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import os
3
+ import gradio as gr
4
+
5
+ # --- Hugging Face Spaces Deployment Specifics ---
6
+
7
+ # Get API key from Spaces secrets
8
+ API_KEY = os.environ.get("GOOGLE_API_KEY")
9
+
10
+ if API_KEY is None:
11
+ raise ValueError("API key not found. Set GOOGLE_API_KEY in Spaces secrets.")
12
+
13
+
14
+
15
+ # --- Gemini Flash Summarization Function ---
16
+
17
+ def summarize_text_with_gemini(text, temperature=0.2, max_decode_steps=1024):
18
+ """Summarizes text using Gemini Flash API."""
19
+
20
+ api_url = "https://generativelanguage.googleapis.com/v1beta3/models/gemini-flash:generateText"
21
+
22
+ headers = {
23
+ "Content-Type": "application/json",
24
+ "Authorization": f"Bearer {API_KEY}"
25
+ }
26
+
27
+ request_body = {
28
+ "prompt": {
29
+ "text": f"Please provide a concise summary of the following text:\n\n{text}"
30
+ },
31
+ "temperature": temperature,
32
+ "maxDecodeSteps": max_decode_steps
33
+ }
34
+
35
+ try:
36
+ response = requests.post(api_url, headers=headers, json=request_body)
37
+ response.raise_for_status()
38
+
39
+ json_response = response.json()
40
+
41
+ if "candidates" in json_response and len(json_response["candidates"]) > 0:
42
+ summary = json_response["candidates"][0]["output"]
43
+ return summary
44
+ else:
45
+ print("No summary candidates found in the API response.") # Good for debugging in logs
46
+ return "No summary could be generated." # More user-friendly
47
+
48
+ except requests.exceptions.RequestException as e:
49
+ print(f"Error during API call: {e}") # For logs/debugging
50
+ return f"An error occurred: {e}" # User-facing message (be cautious about exposing too much detail)
51
+
52
+
53
+
54
+ # --- Hugging Face Spaces Gradio App ---
55
+
56
+ with gr.Blocks() as demo: # Use with statement for proper cleanup
57
+ gr.Markdown("# Gemini Flash Text Summarizer") # Title
58
+
59
+ with gr.Row(): # Arrange input and output side-by-side
60
+ input_text = gr.Textbox(lines=8, label="Input Text")
61
+ output_text = gr.Textbox(lines=8, label="Summary")
62
+
63
+
64
+ temp_slider = gr.Slider(minimum=0.0, maximum=1.0, step=0.1, value=0.2, label="Temperature")
65
+ max_steps_slider = gr.Slider(minimum=64, maximum=2048, step=64, value=1024, label="Max Decode Steps")
66
+
67
+ submit_btn = gr.Button("Summarize")
68
+
69
+
70
+ submit_btn.click(
71
+ fn=summarize_text_with_gemini,
72
+ inputs=[input_text, temp_slider, max_steps_slider], # Pass temperature and max steps
73
+ outputs=output_text,
74
+ api_name="summarize" # Optional: for easily creating shareable links/embedding the demo.
75
+
76
+ )
77
+
78
+ demo.launch()