DECISIONNEWWW / app.py
micole66's picture
Update app.py
cd7e473 verified
import gradio as gr
import google.generativeai as genai
from ddgs import DDGS
import os # Required to read the Hugging Face Secret
# --- PROFESSIONAL DATA RETRIEVAL ---
def get_professional_data(options_list):
collected_data = []
try:
with DDGS() as ddgs:
comp_query = f"professional comparison review {', '.join(options_list)} 2024 2025 benchmarks"
comp_results = list(ddgs.text(comp_query, max_results=5))
for r in comp_results:
collected_data.append(f"[Comparison Source]: {r['body']}")
for opt in options_list:
tech_query = f"{opt} technical specifications pros and cons professional analysis"
tech_results = list(ddgs.text(tech_query, max_results=3))
for r in tech_results:
collected_data.append(f"[Technical Source - {opt}]: {r['body']}")
except Exception as e:
return f"Search connection error: {e}"
return "\n".join(collected_data)
# --- MAIN PROCESSING FUNCTION ---
def decide_best_option(options_input, use_web):
# 1. GET KEY DIRECTLY FROM SECRETS (No UI input needed)
api_key = os.getenv("GOOGLE_API_KEY")
if not api_key:
return "❌ System Error: API Key not found in environment secrets.", ""
if not options_input.strip():
return "⚠️ Warning: Please enter at least one option.", ""
try:
genai.configure(api_key=api_key)
try:
model = genai.GenerativeModel('gemma-4-31b-it')
except:
available = [m.name for m in genai.list_models() if 'gemma' in m.name.lower()]
model = genai.GenerativeModel(available[0] if available else 'gemini-1.5-flash')
options_list = [o.strip() for o in options_input.split('\n') if o.strip()]
web_data = ""
if use_web:
web_data = get_professional_data(options_list)
prompt = f"""
You are Gemma 4 31B IT, a professional analytical expert.
OPTIONS TO COMPARE: {options_input}
RESEARCH DATA: {web_data if web_data else "No external data provided."}
INSTRUCTIONS: 1. Analyze Research Data. 2. Focus on specs/benchmarks. 3. Select absolute winner.
OUTPUT FORMAT:
ANALYSIS: [Detailed professional paragraph]
WINNER: [Only the name of the chosen option]
"""
response = model.generate_content(prompt)
res_text = response.text
keyword = "WINNER:"
if keyword in res_text:
parts = res_text.split(keyword)
winner = parts[-1].strip()
analysis = " ".join(parts[:-1]).replace("ANALYSIS:", "").strip()
else:
winner = "Could not determine winner"
analysis = res_text.strip()
return winner, analysis
except Exception as e:
return f"System Error: {e}", ""
# --- GRADIO UI DESIGN ---
custom_css = """
#winner-box {
font-size: 32px !important;
font-weight: bold !important;
text-align: center !important;
color: #1E293B !important;
border: 4px solid #E2E8F0 !important;
border-radius: 15px !important;
background-color: #FFFFFF !important;
}
#analysis-box {
text-align: left !important;
font-style: italic !important;
color: #475569 !important;
}
"""
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
gr.Markdown("# 🎯 Gemma 4 Decision Engine")
gr.Markdown("High-accuracy choices based on technical research and Gemma 4 31B IT.")
with gr.Row():
with gr.Column(scale=1):
# API KEY TEXTBOX REMOVED FROM HERE
search_checkbox = gr.Checkbox(label="Enable Web Research 🌐", value=True)
options_input = gr.Textbox(
label="Enter Options (One per line)",
placeholder="iPhone 16 Pro\nSamsung S24 Ultra\nPixel 9 Pro",
lines=7
)
submit_btn = gr.Button("Determine Best Option", variant="primary")
with gr.Column(scale=1):
winner_output = gr.Textbox(
label="πŸ† THE WINNER",
elem_id="winner-box",
interactive=False
)
analysis_output = gr.Markdown(
label="πŸ“„ Professional Analysis",
elem_id="analysis-box",
value="Analysis will appear here..."
)
# Trigger updated to only send options and checkbox
submit_btn.click(
fn=decide_best_option,
inputs=[options_input, search_checkbox],
outputs=[winner_output, analysis_output]
)
if __name__ == "__main__":
demo.launch()