Futuresony commited on
Commit
f43a4a2
·
verified ·
1 Parent(s): 669f0b6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -22
app.py CHANGED
@@ -1,15 +1,28 @@
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
- from web import search # Web search for fetching real-time answers
4
 
5
  client = InferenceClient("Futuresony/future_ai_12_10_2024.gguf")
6
 
7
- # Define topics that your model was NOT fine-tuned on
8
- OUT_OF_SCOPE_TOPICS = ["current events", "latest news", "sports scores", "politics", "celebrity gossip"]
 
 
 
9
 
10
- def is_out_of_scope(question):
11
- """Check if the question relates to topics the model wasn't trained on."""
12
- return any(topic in question.lower() for topic in OUT_OF_SCOPE_TOPICS)
 
 
 
 
 
 
 
 
 
 
13
 
14
  def google_search(query):
15
  """Fetch search results from Google."""
@@ -38,28 +51,21 @@ def respond(
38
 
39
  response = ""
40
 
41
- response_obj = client.chat_completion(
42
  messages,
43
  max_tokens=max_tokens,
 
44
  temperature=temperature,
45
  top_p=top_p,
46
- logprobs=True, # Get log probabilities to measure uncertainty
47
- )
48
-
49
- # Extract response text
50
- for message in response_obj:
51
  token = message.choices[0].delta.content
52
  response += token
53
- yield response # Stream response
54
-
55
- # Analyze uncertainty (using log probabilities)
56
- logprobs = response_obj.choices[0].logprobs.token_logprobs
57
- avg_confidence = sum(logprobs) / len(logprobs) if logprobs else 0
58
-
59
- # If confidence is low OR the question is about out-of-scope topics, use Google
60
- if avg_confidence < -5 or is_out_of_scope(message):
61
  google_response = google_search(message)
62
- yield f"🤖 AI (Low confidence): {response}\n\n🌍 Google: {google_response}"
63
 
64
  demo = gr.ChatInterface(
65
  respond,
@@ -79,4 +85,3 @@ demo = gr.ChatInterface(
79
 
80
  if __name__ == "__main__":
81
  demo.launch()
82
-
 
1
  import gradio as gr
2
  from huggingface_hub import InferenceClient
3
+ from web import search # Import web search tool
4
 
5
  client = InferenceClient("Futuresony/future_ai_12_10_2024.gguf")
6
 
7
+ def is_uncertain(question, response):
8
+ """Check if the model's response is unreliable."""
9
+ # 1. If response length is too short, it's likely a guess.
10
+ if len(response.split()) < 4:
11
+ return True
12
 
13
+ # 2. If response repeats the question, it might be unsure.
14
+ if response.lower() in question.lower():
15
+ return True
16
+
17
+ # 3. If the response contains generic phrases like "Kulingana na utafiti" (According to research)
18
+ uncertain_phrases = [
19
+ "Kulingana na utafiti", "Inaaminika kuwa", "Ninadhani",
20
+ "It is believed that", "Some people say", "Inasemekana kuwa"
21
+ ]
22
+ if any(phrase.lower() in response.lower() for phrase in uncertain_phrases):
23
+ return True
24
+
25
+ return False
26
 
27
  def google_search(query):
28
  """Fetch search results from Google."""
 
51
 
52
  response = ""
53
 
54
+ for message in client.chat_completion(
55
  messages,
56
  max_tokens=max_tokens,
57
+ stream=True,
58
  temperature=temperature,
59
  top_p=top_p,
60
+ ):
 
 
 
 
61
  token = message.choices[0].delta.content
62
  response += token
63
+ yield response # Stream partial responses
64
+
65
+ # If response is unreliable, fetch from Google
66
+ if is_uncertain(message, response):
 
 
 
 
67
  google_response = google_search(message)
68
+ yield f"🤖 AI: {response}\n\n🌍 Google: {google_response}"
69
 
70
  demo = gr.ChatInterface(
71
  respond,
 
85
 
86
  if __name__ == "__main__":
87
  demo.launch()