NithikaShree commited on
Commit
52a9c7f
·
verified ·
1 Parent(s): 4adcafa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -83
app.py CHANGED
@@ -1,94 +1,42 @@
1
  import os
2
  import gradio as gr
3
  from langchain_groq import ChatGroq
4
- from langchain_tavily import TavilySearch
 
 
 
 
 
 
 
 
5
 
 
6
  groq_api_key = os.environ.get("GROQ_API_KEY")
7
- tavily_api_key = os.environ.get("TAVILY_API_KEY")
8
 
 
9
  llm = ChatGroq(
10
  model_name="openai/gpt-oss-120b",
11
- temperature=0,
12
  groq_api_key=groq_api_key
13
  )
14
 
15
- search = TavilySearch(
16
- max_results=5,
17
- tavily_api_key=tavily_api_key
18
- )
19
-
20
- def search_agent(query):
21
- if not query.strip():
22
- return "Please enter a valid query."
23
-
24
- res = search.invoke(query)
25
- context = "\n".join([r["content"] for r in res.get("results", [])])
26
-
27
- prompt = f"""
28
- Using the following information from web search:
29
- {context}
30
-
31
- Question: {query}
32
- Answer clearly and concisely:
33
- """
34
-
35
- response = llm.invoke(prompt)
36
- return response.content.strip()
37
-
38
- custom_css = """
39
- body {
40
- background: linear-gradient(135deg, #0f2027, #203a43, #2c5364);
41
- }
42
-
43
- .gradio-container {
44
- max-width: 900px !important;
45
- margin: auto;
46
- }
47
-
48
- #title {
49
- font-size: 36px;
50
- font-weight: 700;
51
- text-align: center;
52
- color: white;
53
- }
54
-
55
- #subtitle {
56
- text-align: center;
57
- color: #cfd8dc;
58
- margin-bottom: 20px;
59
- }
60
-
61
- .card {
62
- background: #111827;
63
- border-radius: 16px;
64
- padding: 24px;
65
- box-shadow: 0px 10px 30px rgba(0,0,0,0.4);
66
- }
67
- """
68
-
69
- with gr.Blocks(css=custom_css) as demo:
70
- gr.Markdown("<div id='title'>Nithi's Search Assistant</div>")
71
- gr.Markdown("<div id='subtitle'>Nithi's Search Agent</div>")
72
-
73
- with gr.Column(elem_classes="card"):
74
- query_input = gr.Textbox(
75
- label="Enter your question",
76
- placeholder="Enter your question",
77
- lines=2
78
- )
79
-
80
- search_btn = gr.Button("Search", variant="primary")
81
-
82
- output_box = gr.Textbox(
83
- label="AI Answer",
84
- lines=10,
85
- interactive=False
86
- )
87
-
88
- search_btn.click(
89
- fn=search_agent,
90
- inputs=query_input,
91
- outputs=output_box
92
- )
93
-
94
- demo.launch()
 
1
  import os
2
  import gradio as gr
3
  from langchain_groq import ChatGroq
4
+ from langchain_core.prompts import ChatPromptTemplate
5
+
6
+ system_prompt_ai_teacher = """
7
+ You are Nimi AI, an AI Teacher at Resynclogic - Artificial Intelligence Research Institute.
8
+ Your mission is to teach AI to beginners like you're explaining it to a 10-year-old.
9
+ Use short sentences. Be friendly and encouraging.
10
+ Always ask a small follow-up question.
11
+ Always say you are “Nimi AI – AI Teacher, built at Resynclogic – Artificial Intelligence Research Institute.”
12
+ """
13
 
14
+ # Load API key from Hugging Face Secrets
15
  groq_api_key = os.environ.get("GROQ_API_KEY")
 
16
 
17
+ # Initialize model
18
  llm = ChatGroq(
19
  model_name="openai/gpt-oss-120b",
20
+ temperature=0.7,
21
  groq_api_key=groq_api_key
22
  )
23
 
24
+ # Prompt chain
25
+ prompt = ChatPromptTemplate.from_messages([
26
+ ("system", system_prompt_ai_teacher),
27
+ ("human", "{user_input}")
28
+ ])
29
+
30
+ chain = prompt | llm
31
+
32
+ # Gradio chat function
33
+ def predict(message, history):
34
+ response = chain.invoke({"user_input": message})
35
+ return response.content
36
+
37
+ # Gradio UI
38
+ gr.ChatInterface(
39
+ predict,
40
+ title="NITHI'S CHATBOT – Nimi AI 🤖",
41
+ description="Beginner-friendly AI teacher. Ask anything about AI!"
42
+ ).launch()