File size: 5,223 Bytes
1a43a9c
ae96584
bbf96e8
1a43a9c
 
 
c0e8472
1a43a9c
 
 
c0e8472
1a43a9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c0e8472
1a43a9c
c0e8472
1a43a9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae96584
1a43a9c
 
ae96584
1a43a9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1604d9d
1a43a9c
 
 
 
 
 
ae96584
 
1a43a9c
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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)