mhamoody commited on
Commit
ad95629
·
verified ·
1 Parent(s): 9c4953f
Files changed (1) hide show
  1. app.py +27 -19
app.py CHANGED
@@ -56,9 +56,6 @@ def bot(
56
  text_prompt = item.get("text", "").strip()
57
  break
58
 
59
- # Debug: print what we received
60
- print(f"DEBUG: content={content}, text_prompt='{text_prompt}'")
61
-
62
  # Handle cases for text and/or image input
63
  if not text_prompt and not image_prompt:
64
  chatbot[-1]["content"] = "Prompt cannot be empty. Please provide input text or an image."
@@ -71,25 +68,32 @@ def bot(
71
  # If both text and image are provided, combine them
72
  text_prompt = f"{text_prompt}. Also, analyze the provided image."
73
 
74
- # Configure the model
75
- genai.configure(api_key=google_key)
76
- model_instance = genai.GenerativeModel(model_name, system_instruction=system_instruction_analysis)
77
 
78
- generation_config = genai.types.GenerationConfig(
79
- temperature=temperature,
80
- max_output_tokens=max_output_tokens,
81
- stop_sequences=preprocess_stop_sequences(stop_sequences),
82
- top_k=top_k,
83
- top_p=top_p,
84
- )
 
 
85
 
86
  # Prepare inputs
87
  inputs = [text_prompt] if image_prompt is None else [text_prompt, preprocess_image(image_prompt)]
88
 
89
  # Generate response
90
  try:
91
- response = model_instance.generate_content(inputs, stream=True, generation_config=generation_config)
92
- response.resolve()
 
 
 
 
 
 
93
  except Exception as e:
94
  chatbot[-1]["content"] = f"Error occurred: {str(e)}"
95
  yield chatbot
@@ -97,12 +101,16 @@ def bot(
97
 
98
  # Stream the response back to the chatbot
99
  chatbot.append({"role": "assistant", "content": ""})
100
- for chunk in response:
101
- if chunk.text:
102
- for i in range(0, len(chunk.text), 10):
103
- chatbot[-1]["content"] += chunk.text[i:i + 10]
 
104
  time.sleep(0.01)
105
  yield chatbot
 
 
 
106
  # Components
107
  google_key_component = gr.Textbox(
108
  label="Google API Key",
 
56
  text_prompt = item.get("text", "").strip()
57
  break
58
 
 
 
 
59
  # Handle cases for text and/or image input
60
  if not text_prompt and not image_prompt:
61
  chatbot[-1]["content"] = "Prompt cannot be empty. Please provide input text or an image."
 
68
  # If both text and image are provided, combine them
69
  text_prompt = f"{text_prompt}. Also, analyze the provided image."
70
 
71
+ # Initialize the client with API key
72
+ client = genai.Client(api_key=google_key)
 
73
 
74
+ generation_config = {
75
+ "temperature": temperature,
76
+ "max_output_tokens": max_output_tokens,
77
+ "top_k": top_k,
78
+ "top_p": top_p,
79
+ }
80
+
81
+ if preprocess_stop_sequences(stop_sequences):
82
+ generation_config["stop_sequences"] = preprocess_stop_sequences(stop_sequences)
83
 
84
  # Prepare inputs
85
  inputs = [text_prompt] if image_prompt is None else [text_prompt, preprocess_image(image_prompt)]
86
 
87
  # Generate response
88
  try:
89
+ response = client.models.generate_content(
90
+ model=model_name,
91
+ contents=inputs,
92
+ config=genai.types.GenerateContentConfig(
93
+ system_instruction=system_instruction_analysis,
94
+ **generation_config
95
+ ),
96
+ )
97
  except Exception as e:
98
  chatbot[-1]["content"] = f"Error occurred: {str(e)}"
99
  yield chatbot
 
101
 
102
  # Stream the response back to the chatbot
103
  chatbot.append({"role": "assistant", "content": ""})
104
+ try:
105
+ if response.text:
106
+ # Stream the response text character by character
107
+ for i in range(0, len(response.text), 10):
108
+ chatbot[-1]["content"] += response.text[i:i + 10]
109
  time.sleep(0.01)
110
  yield chatbot
111
+ except Exception as e:
112
+ chatbot[-1]["content"] = f"Error processing response: {str(e)}"
113
+ yield chatbot
114
  # Components
115
  google_key_component = gr.Textbox(
116
  label="Google API Key",