mgokg commited on
Commit
9371b38
·
verified ·
1 Parent(s): 966e8dd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -20
app.py CHANGED
@@ -1,11 +1,18 @@
1
  import gradio as gr
2
- import requests
3
  import os
4
- import json
5
  import google.generativeai as genai
 
 
 
 
6
 
7
  # Load environment variables
8
- genai.configure(api_key=os.environ["geminiapikey"])
 
 
 
 
 
9
  read_key = os.environ.get('HF_TOKEN', None)
10
 
11
  custom_css = """
@@ -15,14 +22,14 @@ custom_css = """
15
  background: #202020;
16
  padding: 20px;
17
  color: white;
18
- border: 1 px solid white;
19
  }
20
  """
21
 
22
  def predict(prompt):
23
  # Create the model
24
  generation_config = {
25
- "temperature": 0.3,
26
  "top_p": 0.95,
27
  "top_k": 40,
28
  "max_output_tokens": 2048,
@@ -31,31 +38,35 @@ def predict(prompt):
31
 
32
  model = genai.GenerativeModel(
33
  model_name="gemini-1.5-pro",
34
- #model_name="gemini-2.0-flash-exp",
35
  generation_config=generation_config,
36
  )
37
 
38
- chat_session = model.start_chat(
39
- history=[
40
- ]
41
- )
42
-
43
- response = chat_session.send_message(prompt, tools='google_search_retrieval')
44
- #response = model.generate_content(contents=prompt, tools='google_search_retrieval')
45
- return response.text
 
 
 
 
 
 
46
 
47
  # Create the Gradio interface
48
  with gr.Blocks(css=custom_css) as demo:
49
  with gr.Row():
50
- details_output = gr.Markdown(label="answer", elem_id="md")
51
- #details_output = gr.Textbox(label="Ausgabe", value = f"\n\n\n\n")
 
52
  with gr.Row():
53
- ort_input = gr.Textbox(label="prompt", placeholder="ask anything...")
54
- with gr.Row():
55
- button = gr.Button("Senden")
56
 
57
  # Connect the button to the function
58
- button.click(fn=predict, inputs=ort_input, outputs=details_output)
59
 
60
  # Launch the Gradio application
61
  demo.launch()
 
1
  import gradio as gr
 
2
  import os
 
3
  import google.generativeai as genai
4
+ import logging
5
+
6
+ # Configure Logging
7
+ logging.basicConfig(level=logging.ERROR, format='%(asctime)s - %(levelname)s - %(message)s')
8
 
9
  # Load environment variables
10
+ try:
11
+ genai.configure(api_key=os.environ["geminiapikey"])
12
+ except KeyError:
13
+ logging.error("Error: 'geminiapikey' environment variable not found.")
14
+ exit(1)
15
+
16
  read_key = os.environ.get('HF_TOKEN', None)
17
 
18
  custom_css = """
 
22
  background: #202020;
23
  padding: 20px;
24
  color: white;
25
+ border: 1px solid white;
26
  }
27
  """
28
 
29
  def predict(prompt):
30
  # Create the model
31
  generation_config = {
32
+ "temperature": 0.7,
33
  "top_p": 0.95,
34
  "top_k": 40,
35
  "max_output_tokens": 2048,
 
38
 
39
  model = genai.GenerativeModel(
40
  model_name="gemini-1.5-pro",
 
41
  generation_config=generation_config,
42
  )
43
 
44
+ try:
45
+ contents_to_send = [genai.Content(parts=[prompt])]
46
+
47
+ response = model.generate_content(contents=contents_to_send, tools='google_search_retrieval')
48
+
49
+ if response and response.text:
50
+ return response.text
51
+ else:
52
+ logging.error(f"Unexpected response: {response}")
53
+ return "Error: Could not extract text from the response."
54
+ except Exception as e:
55
+ logging.error(f"An error occurred: {e}")
56
+ return f"An error occurred: {e}"
57
+
58
 
59
  # Create the Gradio interface
60
  with gr.Blocks(css=custom_css) as demo:
61
  with gr.Row():
62
+ details_output = gr.Markdown(label="answer", elem_id="md")
63
+ with gr.Row():
64
+ ort_input = gr.Textbox(label="prompt", placeholder="ask anything...")
65
  with gr.Row():
66
+ button = gr.Button("Senden")
 
 
67
 
68
  # Connect the button to the function
69
+ button.click(fn=predict, inputs=ort_input, outputs=details_output)
70
 
71
  # Launch the Gradio application
72
  demo.launch()