Update app.py
Browse files
app.py
CHANGED
|
@@ -10,6 +10,7 @@ from io import StringIO
|
|
| 10 |
from bs4 import BeautifulSoup
|
| 11 |
import re
|
| 12 |
import google.generativeai as genai
|
|
|
|
| 13 |
|
| 14 |
# Setup
|
| 15 |
st.set_page_config(page_title="Gemini Q&A App", layout="centered")
|
|
@@ -30,7 +31,9 @@ if not GOOGLE_API_KEY:
|
|
| 30 |
st.stop()
|
| 31 |
|
| 32 |
genai.configure(api_key=GOOGLE_API_KEY)
|
| 33 |
-
|
|
|
|
|
|
|
| 34 |
|
| 35 |
# Helper: Extract text from various documents
|
| 36 |
def load_and_extract_text(file_path):
|
|
@@ -133,7 +136,6 @@ if uploaded_files and question:
|
|
| 133 |
try:
|
| 134 |
content_blocks = []
|
| 135 |
|
| 136 |
-
# Add prior Q&A as part of the context
|
| 137 |
if st.session_state.chat_history:
|
| 138 |
history_text = "\n\n".join(
|
| 139 |
f"Q: {entry['question']}\nA: {entry['answer']}"
|
|
@@ -141,17 +143,21 @@ if uploaded_files and question:
|
|
| 141 |
)
|
| 142 |
content_blocks.append({"text": f"Previous conversation:\n{history_text}"})
|
| 143 |
|
| 144 |
-
# Add file text content
|
| 145 |
if combined_context.strip():
|
| 146 |
content_blocks.append({"text": f"Context:\n{combined_context}"})
|
| 147 |
|
| 148 |
-
# Add image blocks
|
| 149 |
content_blocks.extend(image_contents)
|
| 150 |
-
|
| 151 |
-
# Add current question
|
| 152 |
content_blocks.append({"text": f"Question: {question}"})
|
| 153 |
|
| 154 |
-
response =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
st.success("💡 Answer:")
|
| 156 |
st.write(response.text)
|
| 157 |
|
|
|
|
| 10 |
from bs4 import BeautifulSoup
|
| 11 |
import re
|
| 12 |
import google.generativeai as genai
|
| 13 |
+
from google.generativeai.types import Tool, GoogleSearch, GenerateContentConfig
|
| 14 |
|
| 15 |
# Setup
|
| 16 |
st.set_page_config(page_title="Gemini Q&A App", layout="centered")
|
|
|
|
| 31 |
st.stop()
|
| 32 |
|
| 33 |
genai.configure(api_key=GOOGLE_API_KEY)
|
| 34 |
+
client = genai.Client()
|
| 35 |
+
model_id = "gemini-2.0-flash"
|
| 36 |
+
google_search_tool = Tool(google_search=GoogleSearch())
|
| 37 |
|
| 38 |
# Helper: Extract text from various documents
|
| 39 |
def load_and_extract_text(file_path):
|
|
|
|
| 136 |
try:
|
| 137 |
content_blocks = []
|
| 138 |
|
|
|
|
| 139 |
if st.session_state.chat_history:
|
| 140 |
history_text = "\n\n".join(
|
| 141 |
f"Q: {entry['question']}\nA: {entry['answer']}"
|
|
|
|
| 143 |
)
|
| 144 |
content_blocks.append({"text": f"Previous conversation:\n{history_text}"})
|
| 145 |
|
|
|
|
| 146 |
if combined_context.strip():
|
| 147 |
content_blocks.append({"text": f"Context:\n{combined_context}"})
|
| 148 |
|
|
|
|
| 149 |
content_blocks.extend(image_contents)
|
|
|
|
|
|
|
| 150 |
content_blocks.append({"text": f"Question: {question}"})
|
| 151 |
|
| 152 |
+
response = client.models.generate_content(
|
| 153 |
+
model=model_id,
|
| 154 |
+
contents=content_blocks,
|
| 155 |
+
config=GenerateContentConfig(
|
| 156 |
+
tools=[google_search_tool],
|
| 157 |
+
response_modalities=["TEXT"],
|
| 158 |
+
)
|
| 159 |
+
)
|
| 160 |
+
|
| 161 |
st.success("💡 Answer:")
|
| 162 |
st.write(response.text)
|
| 163 |
|