File size: 2,138 Bytes
35500cf
 
 
 
 
 
42ab5b9
926b15a
68c35fd
35500cf
 
 
 
 
 
87f91ca
 
 
35500cf
87f91ca
 
 
197ebbb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1630bfa
197ebbb
 
 
 
 
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
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):
    try:
        # Define the Google Search Tool
        google_search_tool = Tool(google_search=GoogleSearch())
        
        # Make the prompt specific to perfumes and their production
        perfume_prompt = f"""
You are an expert perfumer and fragrance chemist.

Answer the following question strictly in the context of:
- Perfumes and fragrances
- Perfume production and formulation
- Raw materials (natural and synthetic)
- Extraction methods (steam distillation, enfleurage, solvent extraction, CO2 extraction)
- Safety, stability, and IFRA regulations
- Perfume notes, accords, and blending techniques

Question:
{question}
"""

        # Generate the response
        response = client.models.generate_content(
            model=MODEL_ID,
            contents=perfume_prompt,
            config=GenerateContentConfig(tools=[google_search_tool]),
        )

        # Extract AI response and search results
        ai_response = response.text  # AI response as plain text
        search_results = response.candidates[0].grounding_metadata.search_entry_point.rendered_content

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

# Gradio Interface
app = gr.Interface(
    fn=google_search_query,
    inputs=gr.Textbox(lines=2, label="Ask a Question about Perfumes"),
    outputs=[
        gr.Textbox(label="Perfume Expert AI Response"),
        gr.HTML(label="Perfume-Related Search Results"),
    ],
    title="Perfume Production Assistant",
    description="Ask questions about perfumes, fragrance formulation, raw materials, and production techniques. The AI uses Google search to provide accurate, industry-relevant answers.",
)

if __name__ == "__main__":
    app.launch(share=True)