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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -13
app.py CHANGED
@@ -1,11 +1,22 @@
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("Futuresony/future_ai_12_10_2024.gguf")
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  def respond(
11
  message,
@@ -26,23 +37,30 @@ def respond(
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=[
@@ -59,6 +77,6 @@ demo = gr.ChatInterface(
59
  ],
60
  )
61
 
62
-
63
  if __name__ == "__main__":
64
  demo.launch()
 
 
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."""
16
+ results = search(query)
17
+ if results:
18
+ return results[0] # Return the first result
19
+ return "Sorry, I couldn't find an answer on Google."
20
 
21
  def respond(
22
  message,
 
37
  messages.append({"role": "user", "content": message})
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,
66
  additional_inputs=[
 
77
  ],
78
  )
79
 
 
80
  if __name__ == "__main__":
81
  demo.launch()
82
+