File size: 2,898 Bytes
683b7d8
 
 
 
 
 
 
225f2c8
c60349f
683b7d8
2aefee2
683b7d8
3794961
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
985a108
 
 
3794961
985a108
 
 
2aefee2
985a108
2aefee2
 
 
3794961
2aefee2
 
985a108
2aefee2
985a108
 
 
683b7d8
985a108
683b7d8
985a108
3794961
0434528
9b83ad3
3794961
2aefee2
3794961
985a108
7d673ae
2aefee2
985a108
 
 
 
 
 
 
 
 
2aefee2
985a108
 
 
2aefee2
683b7d8
985a108
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
import os
import gradio as gr
from google import genai
from google.genai.types import GenerateContentConfig, GoogleSearch, Tool

# Initialize GenAI Client
API_KEY = os.getenv("GOOGLE_API_KEY")  # Ensure to set this in Hugging Face Secrets
client = genai.Client(api_key=API_KEY)
MODEL_ID = "gemini-2.5-flash"  # Replace with your desired model ID

def google_search_query(question, use_web_search):
    try:
        perfume_prompt = f"""
You are an expert perfumer and fragrance production specialist.

Answer strictly in the context of:
- Perfumes and fine fragrances
- Perfume formulation and production processes
- Natural and synthetic fragrance ingredients
- Extraction techniques (steam distillation, solvent extraction, CO2 extraction, enfleurage)
- Blending, accords, top/middle/base notes
- Stability, safety, and IFRA compliance
- Industrial and artisanal perfume manufacturing

Question:
{question}
"""

        # Generate the AI response without web search initially
        response = client.models.generate_content(
            model=MODEL_ID,
            contents=perfume_prompt,
        )
        ai_response = response.text  # AI response as plain text

        if use_web_search:
            # If user selects web search, redefine the tool and generate response with web search
            google_search_tool = Tool(google_search=GoogleSearch())
            response = client.models.generate_content(
                model=MODEL_ID,
                contents=perfume_prompt,
                config=GenerateContentConfig(tools=[google_search_tool]),
            )
            search_results = response.candidates[0].grounding_metadata.search_entry_point.rendered_content
        else:
            search_results = "Web search not used."

        return ai_response, search_results
    except Exception as e:
        return f"Error: {str(e)}", ""

# Gradio Interface using Chatbot and conditional web search
with gr.Blocks(theme=gr.themes.Glass(secondary_hue="violet")) as app:
    gr.Markdown("# Perfume Production Assistant")
    with gr.Row(): 
        chatbot = gr.Chatbot(label="Perfume Expert Responses")
    with gr.Row():
        question_input = gr.Textbox(lines=2, label="Ask a Question about Perfumes")
        web_search_checkbox = gr.Checkbox(label="Enhance with Web Search", value=False)
    with gr.Row():
        submit_button = gr.Button("Submit")
    
    def update_chatbot(question, use_web_search, chat_log):
        ai_response, search_results = google_search_query(question, use_web_search)
        chat_log.append(("You", question))
        chat_log.append(("AI", ai_response))
        if use_web_search:
            chat_log.append(("AI + Web Search", search_results))
        return chat_log
    
    submit_button.click(
        fn=update_chatbot,
        inputs=[question_input, web_search_checkbox, chatbot],
        outputs=[chatbot]
    )

app.launch()