Spaces:
Runtime error
Runtime error
Update app.py
#1
by
priyachakane
- opened
app.py
CHANGED
|
@@ -4,6 +4,7 @@ from huggingface_hub import InferenceClient
|
|
| 4 |
"""
|
| 5 |
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
|
| 6 |
"""
|
|
|
|
| 7 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
| 9 |
|
|
@@ -15,18 +16,35 @@ def respond(
|
|
| 15 |
temperature,
|
| 16 |
top_p,
|
| 17 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
|
|
|
| 20 |
for val in history:
|
| 21 |
if val[0]:
|
| 22 |
messages.append({"role": "user", "content": val[0]})
|
| 23 |
if val[1]:
|
| 24 |
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
|
|
|
|
| 26 |
messages.append({"role": "user", "content": message})
|
| 27 |
|
| 28 |
response = ""
|
|
|
|
| 29 |
|
|
|
|
| 30 |
for message in client.chat_completion(
|
| 31 |
messages,
|
| 32 |
max_tokens=max_tokens,
|
|
@@ -35,28 +53,70 @@ def respond(
|
|
| 35 |
top_p=top_p,
|
| 36 |
):
|
| 37 |
token = message.choices[0].delta.content
|
| 38 |
-
|
| 39 |
response += token
|
| 40 |
yield response
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
"""
|
| 44 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 45 |
"""
|
|
|
|
| 46 |
demo = gr.ChatInterface(
|
| 47 |
respond,
|
| 48 |
additional_inputs=[
|
| 49 |
-
gr.Textbox(value="You are a
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
],
|
|
|
|
|
|
|
| 60 |
)
|
| 61 |
|
| 62 |
|
|
|
|
| 4 |
"""
|
| 5 |
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
|
| 6 |
"""
|
| 7 |
+
# Using Hugging Face Zephyr 7B for better contextual responses
|
| 8 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 9 |
|
| 10 |
|
|
|
|
| 16 |
temperature,
|
| 17 |
top_p,
|
| 18 |
):
|
| 19 |
+
# Define system message to act as MPSC/UPSC assistant
|
| 20 |
+
system_message = """You are an intelligent and well-informed MPSC/UPSC Assistant Chatbot.
|
| 21 |
+
Your job is to assist users with questions related to:
|
| 22 |
+
- MPSC and UPSC Syllabus
|
| 23 |
+
- Exam Patterns and Timelines
|
| 24 |
+
- Study Materials and References
|
| 25 |
+
- Important Current Affairs
|
| 26 |
+
- Guidance on Optional Subjects
|
| 27 |
+
- Previous Year Question Papers
|
| 28 |
+
- Tips for Prelims, Mains, and Interview Preparation
|
| 29 |
+
Provide relevant and concise answers along with reliable references or study materials.
|
| 30 |
+
"""
|
| 31 |
+
|
| 32 |
messages = [{"role": "system", "content": system_message}]
|
| 33 |
|
| 34 |
+
# Load conversation history for context
|
| 35 |
for val in history:
|
| 36 |
if val[0]:
|
| 37 |
messages.append({"role": "user", "content": val[0]})
|
| 38 |
if val[1]:
|
| 39 |
messages.append({"role": "assistant", "content": val[1]})
|
| 40 |
|
| 41 |
+
# Add user’s latest query
|
| 42 |
messages.append({"role": "user", "content": message})
|
| 43 |
|
| 44 |
response = ""
|
| 45 |
+
references = ""
|
| 46 |
|
| 47 |
+
# Generate chatbot response
|
| 48 |
for message in client.chat_completion(
|
| 49 |
messages,
|
| 50 |
max_tokens=max_tokens,
|
|
|
|
| 53 |
top_p=top_p,
|
| 54 |
):
|
| 55 |
token = message.choices[0].delta.content
|
|
|
|
| 56 |
response += token
|
| 57 |
yield response
|
| 58 |
|
| 59 |
+
# Generate additional references and notes based on user query
|
| 60 |
+
references = generate_references(message)
|
| 61 |
+
if references:
|
| 62 |
+
response += f"\n\n📚 **References & Study Notes:**\n{references}"
|
| 63 |
+
yield response
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def generate_references(query):
|
| 67 |
+
"""Generate references and study notes based on user query."""
|
| 68 |
+
if "syllabus" in query.lower():
|
| 69 |
+
return (
|
| 70 |
+
"- UPSC Syllabus: [Official UPSC Website](https://www.upsc.gov.in)\n"
|
| 71 |
+
"- MPSC Syllabus: [Official MPSC Website](https://mpsc.gov.in)"
|
| 72 |
+
)
|
| 73 |
+
elif "current affairs" in query.lower():
|
| 74 |
+
return (
|
| 75 |
+
"- The Hindu, PIB, Yojana Magazine\n"
|
| 76 |
+
"- Monthly Current Affairs PDFs (Vision IAS, Insights IAS)"
|
| 77 |
+
)
|
| 78 |
+
elif "prelims" in query.lower():
|
| 79 |
+
return (
|
| 80 |
+
"- Prelims Strategy: [UPSC Topper's Insights](https://www.insightsonindia.com)\n"
|
| 81 |
+
"- Previous Year Papers: [Download Here](https://upsc.gov.in/previous-year-papers)"
|
| 82 |
+
)
|
| 83 |
+
elif "mains" in query.lower():
|
| 84 |
+
return (
|
| 85 |
+
"- Mains Answer Writing: [IAS Baba, Forum IAS]\n"
|
| 86 |
+
"- Optional Subject Notes: Refer to standard books (Laxmikanth, Bipan Chandra, etc.)"
|
| 87 |
+
)
|
| 88 |
+
elif "interview" in query.lower():
|
| 89 |
+
return (
|
| 90 |
+
"- Mock Interview Preparation: [Drishti IAS, Vision IAS]\n"
|
| 91 |
+
"- DAF Analysis and Personality Tips"
|
| 92 |
+
)
|
| 93 |
+
else:
|
| 94 |
+
return (
|
| 95 |
+
"- Standard Books for All Subjects: NCERTs, Laxmikanth, Spectrum\n"
|
| 96 |
+
"- Regular Updates on Exam Patterns"
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
|
| 100 |
"""
|
| 101 |
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
|
| 102 |
"""
|
| 103 |
+
# Create Gradio Chat Interface
|
| 104 |
demo = gr.ChatInterface(
|
| 105 |
respond,
|
| 106 |
additional_inputs=[
|
| 107 |
+
gr.Textbox(value="You are a smart MPSC/UPSC Assistant.", label="System message"),
|
| 108 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max Tokens (Length of Response)"),
|
| 109 |
+
gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature (Creativity Level)"),
|
| 110 |
+
gr.Slider(
|
| 111 |
+
minimum=0.1,
|
| 112 |
+
maximum=1.0,
|
| 113 |
+
value=0.95,
|
| 114 |
+
step=0.05,
|
| 115 |
+
label="Top-p (Nucleus Sampling for Better Results)",
|
| 116 |
+
),
|
| 117 |
],
|
| 118 |
+
title="🎯 MPSC/UPSC Assistant Chatbot",
|
| 119 |
+
description="Ask anything related to MPSC/UPSC preparation, syllabus, tips, and study materials. The chatbot provides answers with references and helpful notes for your exam preparation.",
|
| 120 |
)
|
| 121 |
|
| 122 |
|