Spaces:
Sleeping
Sleeping
File size: 4,750 Bytes
83540df cd7e473 83540df cd7e473 83540df cd7e473 83540df cd7e473 83540df cd7e473 83540df cd7e473 83540df cd7e473 83540df cd7e473 83540df | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | 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() |