Update app.py
Browse files
app.py
CHANGED
@@ -1,15 +1,13 @@
|
|
1 |
import base64
|
2 |
import os
|
3 |
-
import gradio as gr
|
4 |
from google import genai
|
5 |
from google.genai import types
|
|
|
6 |
|
7 |
-
def
|
8 |
try:
|
9 |
-
client = genai.Client(
|
10 |
-
|
11 |
-
)
|
12 |
-
|
13 |
model = "gemini-2.5-pro-exp-03-25"
|
14 |
contents = [
|
15 |
types.Content(
|
@@ -24,91 +22,27 @@ def generate_response(user_input):
|
|
24 |
response_mime_type="text/plain",
|
25 |
)
|
26 |
|
27 |
-
|
28 |
for chunk in client.models.generate_content_stream(
|
29 |
model=model,
|
30 |
contents=contents,
|
31 |
config=generate_content_config,
|
32 |
):
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
return full_response
|
38 |
except Exception as e:
|
39 |
-
return f"
|
40 |
-
|
41 |
-
# Custom CSS for better appearance
|
42 |
-
css = """
|
43 |
-
.gradio-container {
|
44 |
-
max-width: 800px;
|
45 |
-
margin: auto;
|
46 |
-
}
|
47 |
-
footer {
|
48 |
-
visibility: hidden;
|
49 |
-
}
|
50 |
-
"""
|
51 |
|
52 |
-
|
53 |
-
|
54 |
-
gr.
|
55 |
-
gr.
|
56 |
-
|
57 |
-
with
|
58 |
-
|
59 |
-
|
60 |
-
label="Your Prompt",
|
61 |
-
placeholder="Type your message here...",
|
62 |
-
lines=5,
|
63 |
-
max_lines=10,
|
64 |
-
interactive=True,
|
65 |
-
container=False,
|
66 |
-
)
|
67 |
-
with gr.Column(scale=3):
|
68 |
-
temperature = gr.Slider(
|
69 |
-
minimum=0.1,
|
70 |
-
maximum=2.0,
|
71 |
-
value=1.0,
|
72 |
-
step=0.1,
|
73 |
-
label="Creativity (Temperature)",
|
74 |
-
interactive=True,
|
75 |
-
)
|
76 |
-
|
77 |
-
submit_btn = gr.Button("Generate Response", variant="primary")
|
78 |
-
clear_btn = gr.Button("Clear", variant="secondary")
|
79 |
-
|
80 |
-
output = gr.Textbox(
|
81 |
-
label="AI Response",
|
82 |
-
placeholder="AI response will appear here...",
|
83 |
-
lines=10,
|
84 |
-
max_lines=20,
|
85 |
-
interactive=False,
|
86 |
-
)
|
87 |
-
|
88 |
-
examples = gr.Examples(
|
89 |
-
examples=[
|
90 |
-
["Explain quantum computing in simple terms"],
|
91 |
-
["Write a short poem about artificial intelligence"],
|
92 |
-
["What are the latest advancements in renewable energy?"],
|
93 |
-
],
|
94 |
-
inputs=user_input,
|
95 |
-
label="Example Prompts",
|
96 |
-
)
|
97 |
-
|
98 |
-
# Event handlers
|
99 |
-
submit_btn.click(
|
100 |
-
fn=generate_response,
|
101 |
-
inputs=user_input,
|
102 |
-
outputs=output,
|
103 |
-
api_name="generate"
|
104 |
-
)
|
105 |
-
|
106 |
-
clear_btn.click(
|
107 |
-
fn=lambda: ("", ""),
|
108 |
-
inputs=[],
|
109 |
-
outputs=[user_input, output],
|
110 |
-
)
|
111 |
|
112 |
-
# Run the app
|
113 |
if __name__ == "__main__":
|
114 |
-
|
|
|
1 |
import base64
|
2 |
import os
|
|
|
3 |
from google import genai
|
4 |
from google.genai import types
|
5 |
+
import gradio as gr
|
6 |
|
7 |
+
def generate_text(user_input):
|
8 |
try:
|
9 |
+
client = genai.Client(api_key=os.environ.get("GEMINI_API_KEY"))
|
10 |
+
|
|
|
|
|
11 |
model = "gemini-2.5-pro-exp-03-25"
|
12 |
contents = [
|
13 |
types.Content(
|
|
|
22 |
response_mime_type="text/plain",
|
23 |
)
|
24 |
|
25 |
+
response = []
|
26 |
for chunk in client.models.generate_content_stream(
|
27 |
model=model,
|
28 |
contents=contents,
|
29 |
config=generate_content_config,
|
30 |
):
|
31 |
+
response.append(chunk.text)
|
32 |
+
|
33 |
+
return ''.join(response)
|
34 |
+
|
|
|
35 |
except Exception as e:
|
36 |
+
return f"Error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
|
38 |
+
iface = gr.Interface(
|
39 |
+
fn=generate_text,
|
40 |
+
inputs=gr.Textbox(lines=4, placeholder="Enter your prompt here...", label="Input Text"),
|
41 |
+
outputs=gr.Textbox(label="Generated Text"),
|
42 |
+
title="Gemini 2.5 Pro Text Generator",
|
43 |
+
description="Generate text using the Gemini 2.5 Pro model with streaming support",
|
44 |
+
allow_flagging="never"
|
45 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
|
|
47 |
if __name__ == "__main__":
|
48 |
+
iface.launch()
|