jdesiree commited on
Commit
08bce1e
ยท
verified ยท
1 Parent(s): 3401e20

Integrated Langchain and replaced the respond() function with prompt templating.

Files changed (1) hide show
  1. app.py +119 -39
app.py CHANGED
@@ -1,13 +1,77 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
 
 
 
3
 
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
 
10
- def respond(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  message,
12
  history: list[tuple[str, str]],
13
  system_message,
@@ -15,40 +79,57 @@ 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,
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
- respond,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
 
 
 
 
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()