Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from langchain_groq import ChatGroq
|
| 4 |
+
from langchain_tavily import TavilySearch
|
| 5 |
+
|
| 6 |
+
# Load API keys from Hugging Face Secrets
|
| 7 |
+
groq_api_key = os.environ.get("GROQ_API_KEY")
|
| 8 |
+
tavily_api_key = os.environ.get("TAVILY_API_KEY")
|
| 9 |
+
|
| 10 |
+
# Initialize LLM
|
| 11 |
+
llm = ChatGroq(
|
| 12 |
+
model_name="openai/gpt-oss-120b",
|
| 13 |
+
temperature=0,
|
| 14 |
+
groq_api_key=groq_api_key
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
# Initialize Search
|
| 18 |
+
search = TavilySearch(
|
| 19 |
+
max_results=5,
|
| 20 |
+
tavily_api_key=tavily_api_key
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
def google_style_search(query):
|
| 24 |
+
if not query.strip():
|
| 25 |
+
return "Please enter a search query."
|
| 26 |
+
|
| 27 |
+
res = search.invoke(query)
|
| 28 |
+
context = "\n".join([r["content"] for r in res.get("results", [])])
|
| 29 |
+
|
| 30 |
+
prompt = f"""
|
| 31 |
+
Using the following web search information:
|
| 32 |
+
{context}
|
| 33 |
+
|
| 34 |
+
Question: {query}
|
| 35 |
+
Answer in a clear, user-friendly manner:
|
| 36 |
+
"""
|
| 37 |
+
|
| 38 |
+
response = llm.invoke(prompt)
|
| 39 |
+
return response.content.strip()
|
| 40 |
+
|
| 41 |
+
google_css = """
|
| 42 |
+
body {
|
| 43 |
+
background-color: #ffffff;
|
| 44 |
+
font-family: Arial, sans-serif;
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
.gradio-container {
|
| 48 |
+
max-width: 800px !important;
|
| 49 |
+
margin-top: 60px;
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
#logo {
|
| 53 |
+
font-size: 48px;
|
| 54 |
+
font-weight: 700;
|
| 55 |
+
text-align: center;
|
| 56 |
+
margin-bottom: 30px;
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
#logo span:nth-child(1) { color: #4285F4; }
|
| 60 |
+
#logo span:nth-child(2) { color: #DB4437; }
|
| 61 |
+
#logo span:nth-child(3) { color: #F4B400; }
|
| 62 |
+
#logo span:nth-child(4) { color: #4285F4; }
|
| 63 |
+
#logo span:nth-child(5) { color: #0F9D58; }
|
| 64 |
+
#logo span:nth-child(6) { color: #DB4437; }
|
| 65 |
+
"""
|
| 66 |
+
|
| 67 |
+
with gr.Blocks(css=google_css) as demo:
|
| 68 |
+
gr.Markdown("""
|
| 69 |
+
<div id="logo">
|
| 70 |
+
<span>G</span><span>o</span><span>o</span><span>g</span><span>l</span><span>e</span>
|
| 71 |
+
</div>
|
| 72 |
+
""")
|
| 73 |
+
|
| 74 |
+
query_input = gr.Textbox(
|
| 75 |
+
placeholder="Search anything...",
|
| 76 |
+
lines=1,
|
| 77 |
+
show_label=False
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
search_button = gr.Button("Search")
|
| 81 |
+
|
| 82 |
+
answer_output = gr.Textbox(
|
| 83 |
+
label="Search Result",
|
| 84 |
+
lines=12,
|
| 85 |
+
interactive=False
|
| 86 |
+
)
|
| 87 |
+
|
| 88 |
+
search_button.click(
|
| 89 |
+
fn=google_style_search,
|
| 90 |
+
inputs=query_input,
|
| 91 |
+
outputs=answer_output
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
demo.launch()
|