mgokg commited on
Commit
8a3175b
·
verified ·
1 Parent(s): c1bef8f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -108
app.py CHANGED
@@ -3,57 +3,27 @@ import os
3
  from google import genai
4
  from google.genai import types
5
  import gradio as gr
6
- # removed unused IPython imports
7
- # from IPython.display import display
8
- # from IPython.display import Markdown
9
-
10
- # Ensure the API key environment variable is set
11
- api_key = os.environ.get("GEMINI_API_KEY")
12
- if not api_key:
13
- raise ValueError("GEMINI_API_KEY environment variable not set.")
14
-
15
- # Configure the generative AI client
16
- #genai.configure(api_key=api_key)
17
-
18
- # Note: Using genai.GenerativeModel is the more current practice
19
- # compared to genai.Client().models... but client works too.
20
- # Let's stick to the user's client approach for consistency.
21
- client = genai.Client(api_key=api_key)
22
-
23
- # Define the generation function compatible with Gradio ChatInterface streaming
24
- def generate(message: str, history: list[list[str]]):
25
- """
26
- Generates a response from the Gemini model, streaming the output.
27
-
28
- Args:
29
- message: The latest user message.
30
- history: A list of previous user/assistant message pairs.
31
- Example: [["Hello", "Hi there!"], ["How are you?", "I'm doing well."]]
32
-
33
- Yields:
34
- str: The cumulative response text as it's generated chunk by chunk.
35
- """
36
-
37
- model_name = "gemini-2.5-pro-exp-03-25"
38
- # model_name = "gemini-1.5-flash-latest" # Alternative models
39
- # model_name = "gemini-1.5-pro-latest"
40
- # model_name = "gemini-pro" # Standard pro model
41
-
42
- # --- Construct conversation history for the API ---
43
- # The API expects a list of Content objects (user, model, user, model...)
44
- api_history = []
45
- for user_msg, model_msg in history:
46
- api_history.append(types.Content(role="user", parts=[types.Part.from_text(user_msg)]))
47
- # Ensure model messages are also added correctly
48
- if model_msg: # Avoid adding empty model messages if something went wrong previously
49
- api_history.append(types.Content(role="model", parts=[types.Part.from_text(model_msg)]))
50
-
51
- # Add the current user message to the end
52
- contents = api_history + [
53
- types.Content(role="user", parts=[types.Part.from_text(message)])
54
  ]
55
-
56
- # --- Configure generation parameters ---
57
  tools = [
58
  types.Tool(google_search=types.GoogleSearch())
59
  ]
@@ -61,70 +31,28 @@ def generate(message: str, history: list[list[str]]):
61
  temperature=0.35,
62
  top_p=0.95,
63
  top_k=40,
64
- # max_output_tokens=65536, # Use model's default unless specific limit needed
65
- # max_output_tokens=8192,
66
  tools=tools,
67
  response_mime_type="text/plain",
68
  )
69
 
70
- # --- Call the API and stream the response ---
71
- stream = None # Initialize stream variable
72
- try:
73
- stream = client.generate_content( # Use generate_content with stream=True
74
- model=f"models/{model_name}", # Model name needs 'models/' prefix for generate_content
75
- contents=contents,
76
- generation_config=generate_content_config,
77
- stream=True,
78
- # safety_settings=... # Optional: configure safety settings if needed
79
- )
80
-
81
- full_response = ""
82
- for chunk in stream:
83
- # Check if the chunk contains text parts
84
- # Handle potential errors like blocked content or empty chunks
85
- try:
86
- if chunk.text:
87
- full_response += chunk.text
88
- yield full_response # Yield the cumulative response so far
89
- except ValueError:
90
- # Handle cases where the chunk might be blocked or have no text
91
- # (e.g., safety settings blocked response)
92
- # You could yield an error message or just skip
93
- print(f"Warning: Received chunk without valid text: {chunk.prompt_feedback}")
94
- # yield full_response + "\n\n[Content generation issue]" # Option to show user
95
- pass # Silently ignore chunks without text
96
 
97
- except Exception as e:
98
- print(f"An error occurred during generation: {e}")
99
- # Yield a user-friendly error message
100
- yield f"Sorry, an error occurred: {str(e)}"
101
- finally:
102
- # Although Python generators handle cleanup, explicitly clearing
103
- # resources if needed could go here. For this stream, it's usually automatic.
104
- pass
105
 
106
- # --- Launch the Gradio Interface ---
107
  if __name__ == "__main__":
108
  iface = gr.ChatInterface(
109
- fn=generate, # The generator function handles streaming
110
- title=gr.Markdown("<h1><center>Chatbot with Google Search</center></h1>"), # Centered title
111
- description="Chatbot powered by Gemini and Google Search. Type your message and press Enter.",
112
- chatbot=gr.Chatbot(
113
- height=600, # Adjust chatbot window height
114
- show_copy_button=True,
115
- bubble_full_width=False, # Make bubbles wrap text
116
- ),
117
- textbox=gr.Textbox(placeholder="Ask me anything...", container=False, scale=7),
118
- retry_btn="Retry",
119
- undo_btn="Undo",
120
- clear_btn="Clear",
121
- # Examples can be useful for users
122
- examples=[
123
- "What's the latest news about generative AI?",
124
- "Explain the concept of quantum entanglement simply.",
125
- "Write a short poem about a rainy day.",
126
- "Summarize the main points of the Paris Agreement on climate change.",
127
- ],
128
- cache_examples=False # Disable caching if examples should always trigger a fresh generation
129
  )
130
- iface.launch(share=False) # Set share=True to get a public link (use with caution)
 
3
  from google import genai
4
  from google.genai import types
5
  import gradio as gr
6
+ from IPython.display import display
7
+ from IPython.display import Markdown
8
+
9
+ client = genai.Client(
10
+ api_key=os.environ.get("GEMINI_API_KEY"),
11
+ )
12
+
13
+ def generate(prompt, *args):
14
+
15
+ model = "gemini-2.5-pro-exp-03-25"
16
+ #model = "gemini-2.0-flash"
17
+ #model = "gemini-2.0-pro-exp-02-05"
18
+ #model = "gemini-2.0-flash-thinking-exp-01-21"
19
+ contents = [
20
+ types.Content(
21
+ role="user",
22
+ parts=[
23
+ types.Part.from_text(text=f"{prompt}"),
24
+ ],
25
+ ),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  ]
 
 
27
  tools = [
28
  types.Tool(google_search=types.GoogleSearch())
29
  ]
 
31
  temperature=0.35,
32
  top_p=0.95,
33
  top_k=40,
34
+ max_output_tokens=65536,
35
+ #max_output_tokens=8192,
36
  tools=tools,
37
  response_mime_type="text/plain",
38
  )
39
 
40
+ response = ""
41
+ for chunk in client.models.generate_content_stream(
42
+ model=model,
43
+ contents=contents,
44
+ config=generate_content_config,
45
+ ):
46
+ response += chunk.text
47
+
48
+ display(Markdown(response))
49
+ return response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50
 
 
 
 
 
 
 
 
 
51
 
 
52
  if __name__ == "__main__":
53
  iface = gr.ChatInterface(
54
+ fn=generate,
55
+ title=gr.Markdown("# Chatbot with Google Search"),
56
+ description="Chatbot powered by Gemini-2.5 Pro and Google Search.",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57
  )
58
+ iface.launch()