Spaces:
Sleeping
Sleeping
Modified
Browse filesIntegrated Langchain and replaced the respond() function with prompt templating.
app.py
CHANGED
|
@@ -1,13 +1,77 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
""
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
message,
|
| 12 |
history: list[tuple[str, str]],
|
| 13 |
system_message,
|
|
@@ -15,40 +79,57 @@ def respond(
|
|
| 15 |
temperature,
|
| 16 |
top_p,
|
| 17 |
):
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
for message in client.chat_completion(
|
| 31 |
-
messages,
|
| 32 |
-
max_tokens=max_tokens,
|
| 33 |
-
stream=True,
|
| 34 |
-
temperature=temperature,
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
additional_inputs=[
|
| 49 |
-
gr.Textbox(
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
gr.Slider(
|
| 53 |
minimum=0.1,
|
| 54 |
maximum=1.0,
|
|
@@ -59,6 +140,5 @@ demo = gr.ChatInterface(
|
|
| 59 |
],
|
| 60 |
)
|
| 61 |
|
| 62 |
-
|
| 63 |
if __name__ == "__main__":
|
| 64 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from langchain.prompts import ChatPromptTemplate
|
| 3 |
+
from langchain_community.llms import HuggingFaceHub
|
| 4 |
+
from langchain.schema import HumanMessage, SystemMessage
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
+
# Set up the LangChain model (using the same Zephyr model)
|
| 8 |
+
llm = HuggingFaceHub(
|
| 9 |
+
repo_id="HuggingFaceH4/zephyr-7b-beta",
|
| 10 |
+
model_kwargs={"temperature": 0.7, "max_length": 512}
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
# Create different prompt templates for different subjects
|
| 14 |
+
math_template = ChatPromptTemplate.from_messages([
|
| 15 |
+
("system", """You are an expert math tutor. For every math problem:
|
| 16 |
+
1. Break it down step-by-step
|
| 17 |
+
2. Explain the reasoning behind each step
|
| 18 |
+
3. Show all work clearly
|
| 19 |
+
4. Check your answer
|
| 20 |
+
5. Ask if the student has questions about any step"""),
|
| 21 |
+
("human", "{question}")
|
| 22 |
+
])
|
| 23 |
+
|
| 24 |
+
research_template = ChatPromptTemplate.from_messages([
|
| 25 |
+
("system", """You are a research skills mentor. Help students with:
|
| 26 |
+
- Finding reliable sources
|
| 27 |
+
- Evaluating source credibility
|
| 28 |
+
- Proper citation formats
|
| 29 |
+
- Research strategies
|
| 30 |
+
- Academic writing techniques
|
| 31 |
+
Always provide specific, actionable advice."""),
|
| 32 |
+
("human", "{question}")
|
| 33 |
+
])
|
| 34 |
+
|
| 35 |
+
study_template = ChatPromptTemplate.from_messages([
|
| 36 |
+
("system", """You are a study skills coach. Help students with:
|
| 37 |
+
- Effective study methods
|
| 38 |
+
- Time management
|
| 39 |
+
- Memory techniques
|
| 40 |
+
- Test preparation strategies
|
| 41 |
+
- Learning style optimization
|
| 42 |
+
Provide personalized, practical advice."""),
|
| 43 |
+
("human", "{question}")
|
| 44 |
+
])
|
| 45 |
|
| 46 |
+
general_template = ChatPromptTemplate.from_messages([
|
| 47 |
+
("system", """You are EduBot, a friendly AI learning assistant. You help students with:
|
| 48 |
+
๐ Math problems (step-by-step solutions)
|
| 49 |
+
๐ Research skills (finding sources, citations)
|
| 50 |
+
๐ Study strategies (effective learning techniques)
|
| 51 |
+
๐ ๏ธ Educational tools (learning resources)
|
| 52 |
+
|
| 53 |
+
Always be encouraging, patient, and thorough in your explanations."""),
|
| 54 |
+
("human", "{question}")
|
| 55 |
+
])
|
| 56 |
|
| 57 |
+
def detect_subject(message):
|
| 58 |
+
"""Determine which prompt template to use based on the message"""
|
| 59 |
+
message_lower = message.lower()
|
| 60 |
+
|
| 61 |
+
math_keywords = ['math', 'solve', 'calculate', 'equation', 'formula', 'algebra', 'geometry', 'calculus', 'derivative', 'integral']
|
| 62 |
+
research_keywords = ['research', 'source', 'citation', 'bibliography', 'reference', 'academic', 'paper', 'essay', 'thesis']
|
| 63 |
+
study_keywords = ['study', 'memorize', 'exam', 'test', 'quiz', 'review', 'learn', 'remember']
|
| 64 |
+
|
| 65 |
+
if any(keyword in message_lower for keyword in math_keywords):
|
| 66 |
+
return math_template, "๐งฎ Math Mode"
|
| 67 |
+
elif any(keyword in message_lower for keyword in research_keywords):
|
| 68 |
+
return research_template, "๐ Research Mode"
|
| 69 |
+
elif any(keyword in message_lower for keyword in study_keywords):
|
| 70 |
+
return study_template, "๐ Study Mode"
|
| 71 |
+
else:
|
| 72 |
+
return general_template, "๐ General Mode"
|
| 73 |
+
|
| 74 |
+
def respond_with_langchain(
|
| 75 |
message,
|
| 76 |
history: list[tuple[str, str]],
|
| 77 |
system_message,
|
|
|
|
| 79 |
temperature,
|
| 80 |
top_p,
|
| 81 |
):
|
| 82 |
+
# Select the appropriate template
|
| 83 |
+
template, mode = detect_subject(message)
|
| 84 |
+
|
| 85 |
+
# Format the prompt
|
| 86 |
+
formatted_prompt = template.format_messages(question=message)
|
| 87 |
+
|
| 88 |
+
# Get response from LangChain
|
| 89 |
+
try:
|
| 90 |
+
# Convert to string format for the HuggingFace model
|
| 91 |
+
prompt_text = f"{formatted_prompt[0].content}\n\nHuman: {formatted_prompt[1].content}\n\nAssistant:"
|
| 92 |
+
|
| 93 |
+
response = llm.invoke(prompt_text)
|
| 94 |
+
|
| 95 |
+
# Add mode indicator to response
|
| 96 |
+
full_response = f"*{mode}*\n\n{response}"
|
| 97 |
+
|
| 98 |
+
# Yield the response (for streaming effect)
|
| 99 |
+
yield full_response
|
| 100 |
+
|
| 101 |
+
except Exception as e:
|
| 102 |
+
yield f"Sorry, I encountered an error: {str(e)}"
|
| 103 |
|
| 104 |
+
# Create the Gradio interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
demo = gr.ChatInterface(
|
| 106 |
+
respond_with_langchain,
|
| 107 |
+
title="๐ EduBot - AI Learning Assistant",
|
| 108 |
+
description="""
|
| 109 |
+
Your personal AI tutor powered by LangChain! I automatically adapt my teaching style based on your question:
|
| 110 |
+
|
| 111 |
+
โข ๐งฎ **Math Mode** - Step-by-step problem solving
|
| 112 |
+
โข ๐ **Research Mode** - Source finding and citation help
|
| 113 |
+
โข ๐ **Study Mode** - Learning strategies and test prep
|
| 114 |
+
โข ๐ **General Mode** - All-around educational support
|
| 115 |
+
|
| 116 |
+
Try asking me anything about learning!
|
| 117 |
+
""",
|
| 118 |
+
examples=[
|
| 119 |
+
"Solve the equation 3x + 7 = 22 step by step",
|
| 120 |
+
"How do I find reliable sources for my history paper?",
|
| 121 |
+
"What's the best way to study for a biology exam?",
|
| 122 |
+
"Explain the Pythagorean theorem with an example",
|
| 123 |
+
"How do I cite a website in MLA format?"
|
| 124 |
+
],
|
| 125 |
additional_inputs=[
|
| 126 |
+
gr.Textbox(
|
| 127 |
+
value="You are EduBot, an expert AI learning assistant powered by LangChain prompt templates.",
|
| 128 |
+
label="System message",
|
| 129 |
+
visible=False
|
| 130 |
+
),
|
| 131 |
+
gr.Slider(minimum=1, maximum=1024, value=512, step=1, label="Max new tokens"),
|
| 132 |
+
gr.Slider(minimum=0.1, maximum=2.0, value=0.7, step=0.1, label="Temperature"),
|
| 133 |
gr.Slider(
|
| 134 |
minimum=0.1,
|
| 135 |
maximum=1.0,
|
|
|
|
| 140 |
],
|
| 141 |
)
|
| 142 |
|
|
|
|
| 143 |
if __name__ == "__main__":
|
| 144 |
+
demo.launch()
|