Rajesh Karra commited on
Commit
a910699
·
verified ·
1 Parent(s): a2b4df5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -10
app.py CHANGED
@@ -2,17 +2,14 @@ import gradio as gr
2
  import requests
3
  import os
4
 
5
- # Load Gemini API key securely from Hugging Face Secrets
6
  GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
7
 
8
  def query_gemini(prompt):
9
- url = "https://generativelanguage.googleapis.com/v1/models/gemini-1.5-flash-latest:generateContent"
10
  headers = {
11
  "Content-Type": "application/json"
12
  }
13
- params = {
14
- "key": GEMINI_API_KEY
15
- }
16
  data = {
17
  "contents": [
18
  {
@@ -23,19 +20,19 @@ def query_gemini(prompt):
23
  ]
24
  }
25
 
26
- response = requests.post(url, headers=headers, params=params, json=data)
27
 
28
  try:
29
  return response.json()['candidates'][0]['content']['parts'][0]['text']
30
  except Exception as e:
31
- return f"Error: {e}\nResponse: {response.text}"
32
 
33
  iface = gr.Interface(
34
  fn=query_gemini,
35
- inputs=gr.Textbox(lines=4, placeholder="Ask a question to Gemini..."),
36
  outputs="text",
37
- title="Gemini 1.5 Flash API with Gradio",
38
- description="Interact with Gemini 1.5 Flash through Hugging Face Space"
39
  )
40
 
41
  if __name__ == "__main__":
 
2
  import requests
3
  import os
4
 
5
+ # Securely get the API key from Hugging Face secrets
6
  GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
7
 
8
  def query_gemini(prompt):
9
+ url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key={GEMINI_API_KEY}"
10
  headers = {
11
  "Content-Type": "application/json"
12
  }
 
 
 
13
  data = {
14
  "contents": [
15
  {
 
20
  ]
21
  }
22
 
23
+ response = requests.post(url, headers=headers, json=data)
24
 
25
  try:
26
  return response.json()['candidates'][0]['content']['parts'][0]['text']
27
  except Exception as e:
28
+ return f"Error: {e}\nFull Response: {response.text}"
29
 
30
  iface = gr.Interface(
31
  fn=query_gemini,
32
+ inputs=gr.Textbox(lines=4, placeholder="Ask something..."),
33
  outputs="text",
34
+ title="Gemini 2.0 Flash with Gradio",
35
+ description="Powered by Google's Gemini 2.0 Flash API"
36
  )
37
 
38
  if __name__ == "__main__":