Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,122 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
|
|
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
temperature,
|
| 11 |
-
top_p,
|
| 12 |
-
hf_token: gr.OAuthToken,
|
| 13 |
-
):
|
| 14 |
-
"""
|
| 15 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 16 |
-
"""
|
| 17 |
-
client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
|
| 18 |
|
| 19 |
-
|
|
|
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
messages,
|
| 29 |
-
max_tokens=max_tokens,
|
| 30 |
-
stream=True,
|
| 31 |
-
temperature=temperature,
|
| 32 |
-
top_p=top_p,
|
| 33 |
-
):
|
| 34 |
-
choices = message.choices
|
| 35 |
-
token = ""
|
| 36 |
-
if len(choices) and choices[0].delta.content:
|
| 37 |
-
token = choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
"""
|
| 44 |
-
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
-
"""
|
| 46 |
-
chatbot = gr.ChatInterface(
|
| 47 |
-
respond,
|
| 48 |
-
type="messages",
|
| 49 |
-
additional_inputs=[
|
| 50 |
-
gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
|
| 51 |
-
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
| 52 |
-
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
| 53 |
-
gr.Slider(
|
| 54 |
-
minimum=0.1,
|
| 55 |
-
maximum=1.0,
|
| 56 |
-
value=0.95,
|
| 57 |
-
step=0.05,
|
| 58 |
-
label="Top-p (nucleus sampling)",
|
| 59 |
-
),
|
| 60 |
-
],
|
| 61 |
-
)
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
|
| 70 |
-
demo.launch()
|
|
|
|
| 1 |
+
import os
|
| 2 |
import gradio as gr
|
| 3 |
+
from langchain_groq import ChatGroq
|
| 4 |
+
from langchain_core.prompts import ChatPromptTemplate
|
| 5 |
+
from langchain_tavily import TavilySearch
|
| 6 |
|
| 7 |
+
# =====================================================
|
| 8 |
+
# SYSTEM PROMPT
|
| 9 |
+
# =====================================================
|
| 10 |
+
ai_subhash = """
|
| 11 |
+
You are Subhash AI, a smart and friendly AI Mentor for engineering students.
|
| 12 |
+
Explain concepts clearly and step by step in simple words.
|
| 13 |
|
| 14 |
+
Always:
|
| 15 |
+
- Use simple language
|
| 16 |
+
- Give step-by-step explanations
|
| 17 |
+
- Use examples or analogies
|
| 18 |
+
- Be friendly, patient, and motivating
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
+
After every answer, ask one small follow-up question.
|
| 21 |
+
"""
|
| 22 |
|
| 23 |
+
# =====================================================
|
| 24 |
+
# LOAD SECRETS (HUGGING FACE WAY)
|
| 25 |
+
# =====================================================
|
| 26 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 27 |
+
TAVILY_API_KEY = os.getenv("TAVILY_API_KEY")
|
| 28 |
|
| 29 |
+
# =====================================================
|
| 30 |
+
# MODEL + SEARCH
|
| 31 |
+
# =====================================================
|
| 32 |
+
llm = ChatGroq(
|
| 33 |
+
model_name="openai/gpt-oss-120b",
|
| 34 |
+
temperature=0,
|
| 35 |
+
groq_api_key=GROQ_API_KEY
|
| 36 |
+
)
|
| 37 |
|
| 38 |
+
prompt = ChatPromptTemplate.from_messages([
|
| 39 |
+
("system", ai_subhash),
|
| 40 |
+
("human",
|
| 41 |
+
"Chat history:\n{chat_history}\n\n"
|
| 42 |
+
"Context:\n{context}\n\n"
|
| 43 |
+
"User: {user_input}\n\n"
|
| 44 |
+
"AI:")
|
| 45 |
+
])
|
| 46 |
|
| 47 |
+
chain = prompt | llm
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
+
search = TavilySearch(
|
| 50 |
+
max_result=5,
|
| 51 |
+
tavily_api_key=TAVILY_API_KEY
|
| 52 |
+
)
|
| 53 |
|
| 54 |
+
# =====================================================
|
| 55 |
+
# CHAT LOGIC
|
| 56 |
+
# =====================================================
|
| 57 |
+
def predict(message, history):
|
| 58 |
+
if not message.strip():
|
| 59 |
+
return ""
|
| 60 |
|
| 61 |
+
chat_history = ""
|
| 62 |
+
for h in history:
|
| 63 |
+
chat_history += f"User: {h[0]}\nAI: {h[1]}\n"
|
| 64 |
+
|
| 65 |
+
# Web search
|
| 66 |
+
results = search.invoke(message)
|
| 67 |
+
context = "\n".join(
|
| 68 |
+
r.get("content", "") for r in results.get("result", [])
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
response = chain.invoke({
|
| 72 |
+
"user_input": message,
|
| 73 |
+
"context": context,
|
| 74 |
+
"chat_history": chat_history
|
| 75 |
+
})
|
| 76 |
+
|
| 77 |
+
return response.content
|
| 78 |
+
|
| 79 |
+
# =====================================================
|
| 80 |
+
# UI STYLING
|
| 81 |
+
# =====================================================
|
| 82 |
+
custom_css = """
|
| 83 |
+
body {
|
| 84 |
+
background: radial-gradient(circle at top, #020617, #000000);
|
| 85 |
+
}
|
| 86 |
+
.gr-chatbot {
|
| 87 |
+
background: rgba(2, 6, 23, 0.75);
|
| 88 |
+
border-radius: 18px;
|
| 89 |
+
}
|
| 90 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
+
# =====================================================
|
| 93 |
+
# GRADIO UI
|
| 94 |
+
# =====================================================
|
| 95 |
+
with gr.Blocks(css=custom_css, theme=gr.themes.Soft()) as demo:
|
| 96 |
+
|
| 97 |
+
gr.Markdown("# 🤖 Subhash Chatbot")
|
| 98 |
+
gr.Markdown(
|
| 99 |
+
"<div style='text-align:center;color:#a5b4fc'>AI mentor with memory + web search</div>"
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
chatbot = gr.Chatbot(height=420)
|
| 103 |
+
msg = gr.Textbox(
|
| 104 |
+
placeholder="Ask your question...",
|
| 105 |
+
show_label=False
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
clear = gr.Button("🧹 Clear Chat")
|
| 109 |
+
|
| 110 |
+
def respond(message, chat_history):
|
| 111 |
+
reply = predict(message, chat_history)
|
| 112 |
+
chat_history.append((message, reply))
|
| 113 |
+
return "", chat_history
|
| 114 |
+
|
| 115 |
+
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 116 |
+
clear.click(lambda: [], None, chatbot)
|
| 117 |
|
| 118 |
+
gr.Markdown(
|
| 119 |
+
"<footer style='text-align:center;color:#94a3b8'>Built by Subhash • Powered by Groq + Tavily</footer>"
|
| 120 |
+
)
|
| 121 |
|
| 122 |
+
demo.launch()
|
|
|