Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,11 +1,79 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
| 3 |
-
import os
|
| 4 |
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
def respond(message, history: list[dict[str, str]]):
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
messages = [{"role": "system", "content": "You are a friendly chatbot."}]
|
| 11 |
messages.extend(history)
|
|
@@ -24,80 +92,34 @@ def respond(message, history: list[dict[str, str]]):
|
|
| 24 |
yield response
|
| 25 |
|
| 26 |
|
| 27 |
-
# π
|
| 28 |
-
custom_css = """
|
| 29 |
-
.gradio-container {
|
| 30 |
-
background: linear-gradient(to bottom right, #7E498B, #f5f5f5);
|
| 31 |
-
font-family: 'Segoe UI', sans-serif;
|
| 32 |
-
height: 100vh;
|
| 33 |
-
}
|
| 34 |
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
/* User bubble */
|
| 44 |
-
.user.svelte-1ipelgc {
|
| 45 |
-
background: linear-gradient(135deg, #7E498B, #9b59b6) !important;
|
| 46 |
-
color: white !important;
|
| 47 |
-
border-radius: 18px 18px 0 18px !important;
|
| 48 |
-
padding: 10px 14px !important;
|
| 49 |
-
max-width: 80% !important;
|
| 50 |
-
animation: fadeIn 0.3s ease-in;
|
| 51 |
-
}
|
| 52 |
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
background: #f1f1f1 !important;
|
| 56 |
-
color: #333 !important;
|
| 57 |
-
border-radius: 18px 18px 18px 0 !important;
|
| 58 |
-
padding: 10px 14px !important;
|
| 59 |
-
max-width: 80% !important;
|
| 60 |
-
box-shadow: 0px 2px 6px rgba(0,0,0,0.05) !important;
|
| 61 |
-
animation: fadeIn 0.3s ease-in;
|
| 62 |
-
}
|
| 63 |
-
|
| 64 |
-
/* Input box */
|
| 65 |
-
textarea {
|
| 66 |
-
border-radius: 25px !important;
|
| 67 |
-
padding: 12px 16px !important;
|
| 68 |
-
border: 1px solid #ddd !important;
|
| 69 |
-
outline: none !important;
|
| 70 |
-
font-size: 15px !important;
|
| 71 |
-
resize: none !important;
|
| 72 |
-
}
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
}
|
| 82 |
-
|
| 83 |
-
button:hover {
|
| 84 |
-
background: #632f73 !important;
|
| 85 |
-
}
|
| 86 |
-
|
| 87 |
-
/* Animation */
|
| 88 |
-
@keyframes fadeIn {
|
| 89 |
-
from {opacity: 0; transform: translateY(10px);}
|
| 90 |
-
to {opacity: 1; transform: translateY(0);}
|
| 91 |
-
|
| 92 |
-
/* π Hide Gradio Footer (API, Logo, Settings) */
|
| 93 |
-
footer, .svelte-1yycg3h, .builder-bar, .wrap.svelte-1ipelgc {
|
| 94 |
-
display: none !important;
|
| 95 |
-
visibility: hidden !important;}
|
| 96 |
-
}
|
| 97 |
-
"""
|
| 98 |
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
if __name__ == "__main__":
|
| 103 |
demo.launch(share=True)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from huggingface_hub import InferenceClient
|
|
|
|
| 3 |
|
| 4 |
+
# β
Hardcoded Token & Model
|
| 5 |
+
HF_TOKEN = "hf_your_token_here" # apna HF token paste karein
|
| 6 |
+
MODEL_NAME = "openai/gpt-oss-20b"
|
| 7 |
+
|
| 8 |
+
# π Hardcoded Practical Lists
|
| 9 |
+
hardcoded_answers = {
|
| 10 |
+
"chemistry class 9 practicals": """π¬ Chemistry Class IX (Practical Index)
|
| 11 |
+
|
| 12 |
+
- To separate the given mixture by physical method
|
| 13 |
+
- To determine the melting point of naphthalene
|
| 14 |
+
- To determine the boiling point of acetone
|
| 15 |
+
- To separate naphthalene from mixture of naphthalene and sand (sublimation)
|
| 16 |
+
- To separate alcohol and water by distillation
|
| 17 |
+
- To demonstrate that chemical reaction releases energy in the form of heat
|
| 18 |
+
- To prepare 100 mL of 0.1M sodium hydroxide solution
|
| 19 |
+
- To prepare 250 mL of 0.1M hydrochloric acid solution
|
| 20 |
+
- To prepare 100 mL of 0.1M sodium carbonate solution
|
| 21 |
+
- To prepare 100 mL of 0.1M NaOH by dilution of given solution
|
| 22 |
+
- To prepare pure copper sulphate crystals from impure sample
|
| 23 |
+
- To demonstrate miscible and immiscible liquids
|
| 24 |
+
- To demonstrate effect of temperature on solubility
|
| 25 |
+
- To demonstrate electrical conductivity of different solutions
|
| 26 |
+
- To demonstrate formation of a binary compound""",
|
| 27 |
+
|
| 28 |
+
"chemistry class 10 practicals": """π¬ Chemistry Class X (Practical Index)
|
| 29 |
+
|
| 30 |
+
- Identify sodium, calcium, strontium, barium, copper and potassium ions by flame test
|
| 31 |
+
- Standardize the given HCl solution volumetrically
|
| 32 |
+
- Determine exact molarity of NaβCOβ solution volumetrically
|
| 33 |
+
- Demonstrate that some natural substances are weak acids
|
| 34 |
+
- Classify substances as acidic, basic, or neutral
|
| 35 |
+
- Demonstrate decomposition of sugar into elements/compounds""",
|
| 36 |
+
|
| 37 |
+
"physics class 9 practicals": """β Physics Class IX (Practical Index)
|
| 38 |
+
|
| 39 |
+
- To measure area of cross-section of a cylinder using vernier callipers
|
| 40 |
+
- To measure volume of a solid cylinder using vernier callipers
|
| 41 |
+
- To study motion of a ball rolling down an inclined plane (s vs tΒ² graph)
|
| 42 |
+
- To determine acceleration due to gravity (g) by free fall method
|
| 43 |
+
- To find limiting friction by roller method
|
| 44 |
+
- To find resultant of two forces acting at a point by graphical method
|
| 45 |
+
- To verify principle of moments using a beam balance
|
| 46 |
+
- To study variation of time period of a pendulum with length and calculate g
|
| 47 |
+
- To determine density of a solid heavier than water using Archimedes principle
|
| 48 |
+
- To study temperature vs time graph (ice β water β steam)""",
|
| 49 |
+
|
| 50 |
+
"physics class 10 practicals": """β Physics Class X (Practical Index)
|
| 51 |
+
|
| 52 |
+
- To verify the law of reflection of light
|
| 53 |
+
- To find refractive index of water using a concave mirror
|
| 54 |
+
- To determine critical angle of glass using semicircular slab/prism
|
| 55 |
+
- To trace path of a ray of light through a glass prism and measure deviation
|
| 56 |
+
- To find focal length of convex lens (parallel ray/parallax method)
|
| 57 |
+
- To set up an astronomical telescope
|
| 58 |
+
- To set up a microscope
|
| 59 |
+
- To verify Ohmβs law using wire as a conductor
|
| 60 |
+
- To study resistors in series circuit
|
| 61 |
+
- To study resistors in parallel circuit
|
| 62 |
+
- To determine resistance of a galvanometer by half-deflection method
|
| 63 |
+
- To trace magnetic field using a bar magnet"""
|
| 64 |
+
}
|
| 65 |
+
|
| 66 |
|
| 67 |
def respond(message, history: list[dict[str, str]]):
|
| 68 |
+
# β
Pehle check karein hardcoded answers
|
| 69 |
+
lower_msg = message.lower()
|
| 70 |
+
for key, answer in hardcoded_answers.items():
|
| 71 |
+
if key in lower_msg:
|
| 72 |
+
yield answer
|
| 73 |
+
return # yahan se return ho jayega, model call nahi hoga
|
| 74 |
+
|
| 75 |
+
# β
Agar hardcoded nahi mila toh model se response lo
|
| 76 |
+
client = InferenceClient(token=HF_TOKEN, model=MODEL_NAME)
|
| 77 |
|
| 78 |
messages = [{"role": "system", "content": "You are a friendly chatbot."}]
|
| 79 |
messages.extend(history)
|
|
|
|
| 92 |
yield response
|
| 93 |
|
| 94 |
|
| 95 |
+
# π CSS aur UI wahi rakha jo aapke code me hai
|
| 96 |
+
custom_css = """ ... (same CSS as before) ... """
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
+
with gr.Blocks(css=custom_css) as demo:
|
| 99 |
+
with gr.Column():
|
| 100 |
+
gr.HTML("<div class='top-bar'>π¬ Virtual Chatbot</div>")
|
| 101 |
+
chatbot = gr.Chatbot(elem_classes="chatbot", bubble_full_width=False, show_copy_button=False)
|
| 102 |
+
with gr.Row(elem_classes="input-area"):
|
| 103 |
+
msg = gr.Textbox(placeholder="Type your message...", lines=1)
|
| 104 |
+
send_btn = gr.Button("β€")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
+
def user_submit(user_message, chat_history):
|
| 107 |
+
return "", chat_history + [(user_message, None)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
+
def bot_response(chat_history):
|
| 110 |
+
user_message = chat_history[-1][0]
|
| 111 |
+
response = ""
|
| 112 |
+
for partial in respond(user_message, [{"role": "user", "content": h[0]} for h in chat_history[:-1]]):
|
| 113 |
+
response = partial
|
| 114 |
+
chat_history[-1] = (chat_history[-1][0], response)
|
| 115 |
+
return chat_history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
|
| 117 |
+
msg.submit(user_submit, [msg, chatbot], [msg, chatbot]).then(
|
| 118 |
+
bot_response, chatbot, chatbot
|
| 119 |
+
)
|
| 120 |
+
send_btn.click(user_submit, [msg, chatbot], [msg, chatbot]).then(
|
| 121 |
+
bot_response, chatbot, chatbot
|
| 122 |
+
)
|
| 123 |
|
| 124 |
if __name__ == "__main__":
|
| 125 |
demo.launch(share=True)
|