aimanathar's picture
Update app.py
1a43a9c verified
import gradio as gr
from huggingface_hub import InferenceClient
# βœ… Hardcoded Token & Model
HF_TOKEN = "hf_your_token_here" # apna HF token paste karein
MODEL_NAME = "openai/gpt-oss-20b"
# πŸ”’ Hardcoded Practical Lists
hardcoded_answers = {
"chemistry class 9 practicals": """πŸ”¬ Chemistry Class IX (Practical Index)
- To separate the given mixture by physical method
- To determine the melting point of naphthalene
- To determine the boiling point of acetone
- To separate naphthalene from mixture of naphthalene and sand (sublimation)
- To separate alcohol and water by distillation
- To demonstrate that chemical reaction releases energy in the form of heat
- To prepare 100 mL of 0.1M sodium hydroxide solution
- To prepare 250 mL of 0.1M hydrochloric acid solution
- To prepare 100 mL of 0.1M sodium carbonate solution
- To prepare 100 mL of 0.1M NaOH by dilution of given solution
- To prepare pure copper sulphate crystals from impure sample
- To demonstrate miscible and immiscible liquids
- To demonstrate effect of temperature on solubility
- To demonstrate electrical conductivity of different solutions
- To demonstrate formation of a binary compound""",
"chemistry class 10 practicals": """πŸ”¬ Chemistry Class X (Practical Index)
- Identify sodium, calcium, strontium, barium, copper and potassium ions by flame test
- Standardize the given HCl solution volumetrically
- Determine exact molarity of Naβ‚‚CO₃ solution volumetrically
- Demonstrate that some natural substances are weak acids
- Classify substances as acidic, basic, or neutral
- Demonstrate decomposition of sugar into elements/compounds""",
"physics class 9 practicals": """βš™ Physics Class IX (Practical Index)
- To measure area of cross-section of a cylinder using vernier callipers
- To measure volume of a solid cylinder using vernier callipers
- To study motion of a ball rolling down an inclined plane (s vs tΒ² graph)
- To determine acceleration due to gravity (g) by free fall method
- To find limiting friction by roller method
- To find resultant of two forces acting at a point by graphical method
- To verify principle of moments using a beam balance
- To study variation of time period of a pendulum with length and calculate g
- To determine density of a solid heavier than water using Archimedes principle
- To study temperature vs time graph (ice β†’ water β†’ steam)""",
"physics class 10 practicals": """βš™ Physics Class X (Practical Index)
- To verify the law of reflection of light
- To find refractive index of water using a concave mirror
- To determine critical angle of glass using semicircular slab/prism
- To trace path of a ray of light through a glass prism and measure deviation
- To find focal length of convex lens (parallel ray/parallax method)
- To set up an astronomical telescope
- To set up a microscope
- To verify Ohm’s law using wire as a conductor
- To study resistors in series circuit
- To study resistors in parallel circuit
- To determine resistance of a galvanometer by half-deflection method
- To trace magnetic field using a bar magnet"""
}
def respond(message, history: list[dict[str, str]]):
# βœ… Pehle check karein hardcoded answers
lower_msg = message.lower()
for key, answer in hardcoded_answers.items():
if key in lower_msg:
yield answer
return # yahan se return ho jayega, model call nahi hoga
# βœ… Agar hardcoded nahi mila toh model se response lo
client = InferenceClient(token=HF_TOKEN, model=MODEL_NAME)
messages = [{"role": "system", "content": "You are a friendly chatbot."}]
messages.extend(history)
messages.append({"role": "user", "content": message})
response = ""
for msg in client.chat_completion(
messages,
max_tokens=512,
stream=True,
temperature=0.7,
top_p=0.95
):
if msg.choices and msg.choices[0].delta.content:
response += msg.choices[0].delta.content
yield response
# πŸ’œ CSS aur UI wahi rakha jo aapke code me hai
custom_css = """ ... (same CSS as before) ... """
with gr.Blocks(css=custom_css) as demo:
with gr.Column():
gr.HTML("<div class='top-bar'>πŸ’¬ Virtual Chatbot</div>")
chatbot = gr.Chatbot(elem_classes="chatbot", bubble_full_width=False, show_copy_button=False)
with gr.Row(elem_classes="input-area"):
msg = gr.Textbox(placeholder="Type your message...", lines=1)
send_btn = gr.Button("➀")
def user_submit(user_message, chat_history):
return "", chat_history + [(user_message, None)]
def bot_response(chat_history):
user_message = chat_history[-1][0]
response = ""
for partial in respond(user_message, [{"role": "user", "content": h[0]} for h in chat_history[:-1]]):
response = partial
chat_history[-1] = (chat_history[-1][0], response)
return chat_history
msg.submit(user_submit, [msg, chatbot], [msg, chatbot]).then(
bot_response, chatbot, chatbot
)
send_btn.click(user_submit, [msg, chatbot], [msg, chatbot]).then(
bot_response, chatbot, chatbot
)
if __name__ == "__main__":
demo.launch(share=True)