File size: 2,179 Bytes
e784299
 
2817a22
04a7403
2817a22
e784299
 
 
d1a7a46
 
b4c3447
2817a22
e784299
04a7403
2817a22
 
 
04a7403
 
 
 
 
 
 
d1a7a46
04a7403
d1a7a46
 
 
 
 
 
 
04a7403
 
d1a7a46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1055179
d1a7a46
04a7403
d1a7a46
04a7403
d1a7a46
 
 
 
 
 
04a7403
d1a7a46
 
 
 
04a7403
d1a7a46
 
 
 
 
04a7403
d1a7a46
 
 
 
 
04a7403
e784299
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
import os
import gradio as gr
from langchain_groq import ChatGroq
from langchain_tavily import TavilySearch

groq_api_key = os.environ.get("GROQ_API_KEY")
tavily_api_key = os.environ.get("TAVILY_API_KEY")

if not groq_api_key or not tavily_api_key:
    raise ValueError("❌ API keys not found. Please add them in Hugging Face Secrets.")

llm = ChatGroq(
    model_name="openai/gpt-oss-120b",
    temperature=0,
    groq_api_key=groq_api_key
)

search = TavilySearch(
    max_results=5,
    tavily_api_key=tavily_api_key
)

def search_agent(query):
    if not query.strip():
        return "⚠️ Please enter a valid query."

    results = search.invoke(query)
    contexts = results.get("results", [])

    if not contexts:
        return "❌ No relevant information found."

    context_text = "\n".join([r["content"] for r in contexts])

    prompt = f"""
Using the following information from web search:

{context_text}

Question: {query}
Answer clearly and concisely:
"""

    response = llm.invoke(prompt)
    return response.content.strip()

custom_css = """
body {
    background: linear-gradient(135deg, #0f2027, #203a43, #2c5364);
}

.gradio-container {
    max-width: 900px !important;
    margin: auto;
}

#title {
    font-size: 36px;
    font-weight: 700;
    text-align: center;
    color: white;
}

#subtitle {
    text-align: center;
    color: #cfd8dc;
    margin-bottom: 20px;
}

.card {
    background: #111827;
    border-radius: 16px;
    padding: 24px;
    box-shadow: 0px 10px 30px rgba(0,0,0,0.4);
}
"""

with gr.Blocks(css=custom_css) as demo:

    gr.Markdown("<div id='title'>Nithi's Search Assistant</div>")

    with gr.Column(elem_classes="card"):
        query_input = gr.Textbox(
            label="Enter your question",
            placeholder="Enter your Question",
            lines=2
        )

        search_btn = gr.Button(
            "Search",
            variant="primary"
        )

        output_box = gr.Textbox(
            label="AI Answer",
            lines=10,
            interactive=False
        )

    search_btn.click(
        fn=search_agent,
        inputs=query_input,
        outputs=output_box
    )

demo.launch()