Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -3,13 +3,18 @@ import os
|
|
3 |
import google.generativeai as genai
|
4 |
import logging
|
5 |
import time
|
6 |
-
|
|
|
7 |
|
8 |
# Configure Logging
|
9 |
logging.basicConfig(level=logging.ERROR, format='%(asctime)s - %(levelname)s - %(message)s')
|
10 |
|
11 |
# Load environment variables
|
12 |
-
|
|
|
|
|
|
|
|
|
13 |
|
14 |
read_key = os.environ.get('HF_TOKEN', None)
|
15 |
|
@@ -24,11 +29,13 @@ custom_css = """
|
|
24 |
}
|
25 |
"""
|
26 |
|
27 |
-
|
|
|
|
|
28 |
def predict(prompt):
|
29 |
# Create the model
|
30 |
generation_config = {
|
31 |
-
"temperature": 0.
|
32 |
"top_p": 0.95,
|
33 |
"top_k": 40,
|
34 |
"max_output_tokens": 2048,
|
@@ -40,10 +47,30 @@ def predict(prompt):
|
|
40 |
generation_config=generation_config,
|
41 |
)
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
# Create the Gradio interface
|
49 |
with gr.Blocks(css=custom_css) as demo:
|
|
|
3 |
import google.generativeai as genai
|
4 |
import logging
|
5 |
import time
|
6 |
+
import backoff
|
7 |
+
import google.ai.generativelanguage as glm
|
8 |
|
9 |
# Configure Logging
|
10 |
logging.basicConfig(level=logging.ERROR, format='%(asctime)s - %(levelname)s - %(message)s')
|
11 |
|
12 |
# Load environment variables
|
13 |
+
try:
|
14 |
+
genai.configure(api_key=os.environ["geminiapikey"])
|
15 |
+
except KeyError:
|
16 |
+
logging.error("Error: 'geminiapikey' environment variable not found.")
|
17 |
+
exit(1)
|
18 |
|
19 |
read_key = os.environ.get('HF_TOKEN', None)
|
20 |
|
|
|
29 |
}
|
30 |
"""
|
31 |
|
32 |
+
@backoff.on_exception(backoff.expo,
|
33 |
+
(genai.APIError),
|
34 |
+
max_tries=3)
|
35 |
def predict(prompt):
|
36 |
# Create the model
|
37 |
generation_config = {
|
38 |
+
"temperature": 0.7,
|
39 |
"top_p": 0.95,
|
40 |
"top_k": 40,
|
41 |
"max_output_tokens": 2048,
|
|
|
47 |
generation_config=generation_config,
|
48 |
)
|
49 |
|
50 |
+
try:
|
51 |
+
# Create the tools configuration
|
52 |
+
|
53 |
+
tools_config = glm.ToolConfig(
|
54 |
+
function_declarations=[],
|
55 |
+
search_queries=[prompt],
|
56 |
+
)
|
57 |
+
|
58 |
+
response = model.generate_content(
|
59 |
+
contents=[prompt], # Directly pass the prompt
|
60 |
+
tools=[tools_config]
|
61 |
+
)
|
62 |
+
|
63 |
+
if response and response.text:
|
64 |
+
return response.text
|
65 |
+
else:
|
66 |
+
logging.error(f"Unexpected response: {response}")
|
67 |
+
return "Error: Could not extract text from the response."
|
68 |
+
except genai.APIError as e:
|
69 |
+
logging.error(f"API error occurred: {e}")
|
70 |
+
raise
|
71 |
+
except Exception as e:
|
72 |
+
logging.error(f"An error occurred: {e}")
|
73 |
+
return f"An error occurred: {e}"
|
74 |
|
75 |
# Create the Gradio interface
|
76 |
with gr.Blocks(css=custom_css) as demo:
|