jarguello76 commited on
Commit
90ea5c3
·
verified ·
1 Parent(s): 76fbe49

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -97
app.py CHANGED
@@ -1,5 +1,3 @@
1
- # TOOLS
2
-
3
  import os
4
  import gradio as gr
5
  import logging
@@ -10,28 +8,36 @@ from llama_index.core.tools import FunctionTool
10
  from duckduckgo_search import DDGS
11
  from sentence_transformers import SentenceTransformer
12
  from sklearn.metrics.pairwise import cosine_similarity
13
- from llama_index.llms.huggingface_api import HuggingFaceInferenceAPI
14
-
15
  import numpy as np
 
 
16
 
17
- # Load HF_TOKEN from environment
18
- HF_TOKEN = os.environ.get("HF_TOKEN")
19
-
20
  logging.basicConfig(level=logging.INFO)
21
  logger = logging.getLogger(__name__)
22
 
23
- # === Core Components ===
24
- class QuestionValidation:
25
- def __init__(self, hf_token: str):
26
- self.client = InferenceClient(model="HuggingFaceH4/zephyr-7b-beta", token=HF_TOKEN)
27
 
28
- self.llm2_model = "HuggingFaceH4/zephyr-7b-beta"
29
- self.embedding_model = SentenceTransformer("sentence-transformers/paraphrase-MiniLM-L3-v2")
 
 
 
 
 
 
 
 
 
 
 
 
30
 
31
  def guess_question(self, answer: str) -> str:
32
  prompt = f"This was the answer: {answer}\nWhat question would likely have led to it?"
33
- response = self.client.text_generation(prompt=prompt, model=self.llm2_model, max_new_tokens=100)
34
- return response
35
 
36
  def compute_similarity(self, q1: str, q2: str) -> float:
37
  embeddings = self.embedding_model.encode([q1, q2])
@@ -45,17 +51,14 @@ class QuestionValidation:
45
  "similarity": round(float(similarity), 4)
46
  }
47
 
 
48
  def search_web(query: str, max_results: int = 5) -> List[Dict[str, str]]:
49
  try:
50
  with DDGS() as ddgs:
51
- results = [r for r in ddgs.text(query, max_results=max_results)]
52
- return results
53
  except Exception as e:
54
  return [{"error": str(e)}]
55
 
56
- import ast
57
- import operator as op
58
-
59
  OPERATORS = {
60
  ast.Add: op.add,
61
  ast.Sub: op.sub,
@@ -71,29 +74,23 @@ OPERATORS = {
71
  def evaluate_math_expression(expr: str) -> str:
72
  try:
73
  node = ast.parse(expr, mode="eval")
74
-
75
  def _eval(node):
76
  if isinstance(node, ast.Expression):
77
  return _eval(node.body)
78
- elif isinstance(node, ast.Num):
79
- return node.n
80
- elif isinstance(node, ast.Constant):
81
- if isinstance(node.value, (int, float)):
82
- return node.value
83
  elif isinstance(node, ast.BinOp):
84
  return OPERATORS[type(node.op)](_eval(node.left), _eval(node.right))
85
  elif isinstance(node, ast.UnaryOp):
86
  return OPERATORS[type(node.op)](_eval(node.operand))
87
  else:
88
  raise ValueError(f"Unsupported expression: {ast.dump(node)}")
89
-
90
- result = _eval(node)
91
- return str(result)
92
  except Exception as e:
93
  return f"Error evaluating expression: {e}"
94
 
95
-
96
- validator = QuestionValidation(hf_token=HF_TOKEN)
97
 
98
  validate_tool = FunctionTool.from_defaults(
99
  fn=validator.validate_question_only,
@@ -115,20 +112,6 @@ math_tool = FunctionTool.from_defaults(
115
 
116
  TOOLS = [validate_tool, search_tool, math_tool]
117
 
118
- from llama_index.core.agent import ReActAgent
119
-
120
-
121
- from llama_index.llms.huggingface import HuggingFaceLLM
122
-
123
- llm = HuggingFaceLLM(
124
- context_window=4096,
125
- max_new_tokens=512,
126
- generate_kwargs={"temperature": 0.7, "top_p": 0.95},
127
- tokenizer_name="HuggingFaceH4/zephyr-7b-beta",
128
- model_name="HuggingFaceH4/zephyr-7b-beta",
129
-
130
- )
131
-
132
  agent = ReActAgent.from_tools(
133
  tools=TOOLS,
134
  llm=llm,
@@ -136,6 +119,7 @@ agent = ReActAgent.from_tools(
136
  max_iterations=3
137
  )
138
 
 
139
  def question_loop_agent(user_question: str):
140
  llm_answer = llm.complete(user_question).text.strip()
141
  similarity_score = 0.0
@@ -156,57 +140,42 @@ def question_loop_agent(user_question: str):
156
  f"Similarity Score: {similarity_score:.4f}"
157
  )
158
 
159
- iface_loop = gr.Interface(
160
- fn=question_loop_agent,
161
- inputs=gr.Textbox(lines=2, placeholder="Ask me a question..."),
162
- outputs="text",
163
- title="Question Similarity Loop Agent",
164
- description="Loops until the guessed question has a similarity score > 0.6."
165
- )
166
-
167
-
168
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
169
-
170
- def respond(
171
- message,
172
- history: list[tuple[str, str]],
173
- system_message,
174
- max_tokens,
175
- temperature,
176
- top_p,
177
- ):
178
- messages = [{"role": "system", "content": system_message}]
179
-
180
- for val in history:
181
- if val[0]:
182
- messages.append({"role": "user", "content": val[0]})
183
- if val[1]:
184
- messages.append({"role": "assistant", "content": val[1]})
185
-
186
- messages.append({"role": "user", "content": message})
187
- response = ""
188
-
189
- for message in client.chat_completion(
190
- messages,
191
- max_tokens=max_tokens,
192
- stream=True,
193
- temperature=temperature,
194
- top_p=top_p,
195
- ):
196
- token = message.choices[0].delta.content
197
- response += token
198
- yield response
199
-
200
- demo = gr.ChatInterface(
201
- respond,
202
- additional_inputs=[
203
- gr.Textbox(value="You are a Chatbot.", label="System message"),
204
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
205
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
206
- gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p (nucleus sampling)"),
207
- ],
208
- )
209
 
210
  if __name__ == "__main__":
211
- iface_loop.launch()
212
- demo.launch() # Uncomment to run the chat template toon
 
 
 
1
  import os
2
  import gradio as gr
3
  import logging
 
8
  from duckduckgo_search import DDGS
9
  from sentence_transformers import SentenceTransformer
10
  from sklearn.metrics.pairwise import cosine_similarity
11
+ from llama_index.llms.huggingface import HuggingFaceLLM
12
+ from llama_index.core.agent import ReActAgent
13
  import numpy as np
14
+ import ast
15
+ import operator as op
16
 
17
+ # === Setup ===
 
 
18
  logging.basicConfig(level=logging.INFO)
19
  logger = logging.getLogger(__name__)
20
 
21
+ HF_TOKEN = os.environ.get("HF_TOKEN")
 
 
 
22
 
23
+ # === LLM Setup (Shared) ===
24
+ llm = HuggingFaceLLM(
25
+ context_window=4096,
26
+ max_new_tokens=512,
27
+ generate_kwargs={"temperature": 0.7, "top_p": 0.95},
28
+ tokenizer_name="HuggingFaceH4/zephyr-7b-beta",
29
+ model_name="HuggingFaceH4/zephyr-7b-beta",
30
+ )
31
+
32
+ # === Question Validation Component ===
33
+ class QuestionValidation:
34
+ def __init__(self, llm_client):
35
+ self.client = llm_client
36
+ self.embedding_model = SentenceTransformer("all-MiniLM-L6-v2")
37
 
38
  def guess_question(self, answer: str) -> str:
39
  prompt = f"This was the answer: {answer}\nWhat question would likely have led to it?"
40
+ return self.client.complete(prompt).text.strip()
 
41
 
42
  def compute_similarity(self, q1: str, q2: str) -> float:
43
  embeddings = self.embedding_model.encode([q1, q2])
 
51
  "similarity": round(float(similarity), 4)
52
  }
53
 
54
+ # === Tools ===
55
  def search_web(query: str, max_results: int = 5) -> List[Dict[str, str]]:
56
  try:
57
  with DDGS() as ddgs:
58
+ return [r for r in ddgs.text(query, max_results=max_results)]
 
59
  except Exception as e:
60
  return [{"error": str(e)}]
61
 
 
 
 
62
  OPERATORS = {
63
  ast.Add: op.add,
64
  ast.Sub: op.sub,
 
74
  def evaluate_math_expression(expr: str) -> str:
75
  try:
76
  node = ast.parse(expr, mode="eval")
 
77
  def _eval(node):
78
  if isinstance(node, ast.Expression):
79
  return _eval(node.body)
80
+ elif isinstance(node, ast.Constant):
81
+ return node.value
 
 
 
82
  elif isinstance(node, ast.BinOp):
83
  return OPERATORS[type(node.op)](_eval(node.left), _eval(node.right))
84
  elif isinstance(node, ast.UnaryOp):
85
  return OPERATORS[type(node.op)](_eval(node.operand))
86
  else:
87
  raise ValueError(f"Unsupported expression: {ast.dump(node)}")
88
+ return str(_eval(node))
 
 
89
  except Exception as e:
90
  return f"Error evaluating expression: {e}"
91
 
92
+ # Instantiate validator using shared LLM
93
+ validator = QuestionValidation(llm)
94
 
95
  validate_tool = FunctionTool.from_defaults(
96
  fn=validator.validate_question_only,
 
112
 
113
  TOOLS = [validate_tool, search_tool, math_tool]
114
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
  agent = ReActAgent.from_tools(
116
  tools=TOOLS,
117
  llm=llm,
 
119
  max_iterations=3
120
  )
121
 
122
+ # === Question Loop Logic ===
123
  def question_loop_agent(user_question: str):
124
  llm_answer = llm.complete(user_question).text.strip()
125
  similarity_score = 0.0
 
140
  f"Similarity Score: {similarity_score:.4f}"
141
  )
142
 
143
+ # === Gradio Interfaces ===
144
+ with gr.Blocks() as app:
145
+ with gr.Tab("Validation Loop"):
146
+ gr.Interface(
147
+ fn=question_loop_agent,
148
+ inputs=gr.Textbox(lines=2, placeholder="Ask me a question..."),
149
+ outputs="text",
150
+ title="Question Similarity Loop Agent",
151
+ description="Loops until the guessed question has a similarity score > 0.6."
152
+ ).render()
153
+
154
+ with gr.Tab("Chat"):
155
+ def respond(message, history, system_message, max_tokens, temperature, top_p):
156
+ messages = [{"role": "system", "content": system_message}]
157
+ for user, bot in history:
158
+ if user:
159
+ messages.append({"role": "user", "content": user})
160
+ if bot:
161
+ messages.append({"role": "assistant", "content": bot})
162
+ messages.append({"role": "user", "content": message})
163
+
164
+ response = ""
165
+ for msg in llm.chat(messages, stream=True):
166
+ token = msg.delta or ""
167
+ response += token
168
+ yield response
169
+
170
+ gr.ChatInterface(
171
+ respond,
172
+ additional_inputs=[
173
+ gr.Textbox(value="You are a Chatbot.", label="System message"),
174
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
175
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
176
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p")
177
+ ],
178
+ ).render()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
179
 
180
  if __name__ == "__main__":
181
+ app.launch()