Rajesh Karra
commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,17 +2,14 @@ import gradio as gr
|
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
|
| 5 |
-
#
|
| 6 |
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 7 |
|
| 8 |
def query_gemini(prompt):
|
| 9 |
-
url = "https://generativelanguage.googleapis.com/
|
| 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,
|
| 27 |
|
| 28 |
try:
|
| 29 |
return response.json()['candidates'][0]['content']['parts'][0]['text']
|
| 30 |
except Exception as e:
|
| 31 |
-
return f"Error: {e}\
|
| 32 |
|
| 33 |
iface = gr.Interface(
|
| 34 |
fn=query_gemini,
|
| 35 |
-
inputs=gr.Textbox(lines=4, placeholder="Ask
|
| 36 |
outputs="text",
|
| 37 |
-
title="Gemini
|
| 38 |
-
description="
|
| 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__":
|