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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -7
app.py CHANGED
@@ -1,39 +1,56 @@
 
 
 
 
1
  import gradio as gr
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
  {
16
  "parts": [
17
- {"text": prompt}
18
  ]
19
  }
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__":
39
  iface.launch()
 
1
+ # We are importing three things we need:
2
+ # 1. gradio - to make a simple website interface
3
+ # 2. requests - to talk to Gemini API on the internet
4
+ # 3. os - to get our secret API key safely
5
  import gradio as gr
6
  import requests
7
  import os
8
 
9
+ # We get our secret Gemini API key from the environment.
10
+ # This is like unlocking the door so we can talk to the Gemini robot.
11
  GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
12
 
13
+ # This function sends your question (prompt) to Gemini and gets an answer back
14
  def query_gemini(prompt):
15
+ # This is the website link (URL) where we send our question
16
  url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent?key={GEMINI_API_KEY}"
17
+
18
+ # We tell the Gemini robot that we are sending text in JSON format
19
  headers = {
20
  "Content-Type": "application/json"
21
  }
22
+
23
+ # We put our prompt (question) inside a special box (dictionary)
24
  data = {
25
  "contents": [
26
  {
27
  "parts": [
28
+ {"text": prompt} # This is the question the user types
29
  ]
30
  }
31
  ]
32
  }
33
 
34
+ # We send the data to Gemini using a POST request
35
  response = requests.post(url, headers=headers, json=data)
36
 
37
+ # Now we try to get the answer from Gemini’s reply
38
  try:
39
+ # Gemini replies with some candidates, and we take the first answer
40
  return response.json()['candidates'][0]['content']['parts'][0]['text']
41
  except Exception as e:
42
+ # If something goes wrong, we show an error
43
  return f"Error: {e}\nFull Response: {response.text}"
44
 
45
+ # Here we build the user interface using Gradio
46
  iface = gr.Interface(
47
+ fn=query_gemini, # This is the function we call when the user asks something
48
+ inputs=gr.Textbox(lines=4, placeholder="Ask something..."), # Textbox for input
49
+ outputs="text", # The answer will be shown as plain text
50
+ title="Gemini 2.0 Flash with Gradio", # Title of the web app
51
+ description="Powered by Google's Gemini 2.0 Flash API" # A short description
52
  )
53
 
54
+ # This line says: "Run the app" if you run this file directly
55
  if __name__ == "__main__":
56
  iface.launch()