Futuresony commited on
Commit
d6a98e2
·
verified ·
1 Parent(s): 72a9788

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +94 -0
app.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import importlib
4
+ import pytz
5
+ from datetime import datetime
6
+ from bs4 import BeautifulSoup
7
+ from huggingface_hub import InferenceClient
8
+
9
+ # Import weather script
10
+ weather = importlib.import_module("weather")
11
+
12
+ # Hugging Face model
13
+ client = InferenceClient("Futuresony/future_ai_12_10_2024.gguf")
14
+
15
+ def google_search(query):
16
+ """Scrape Google search for an answer."""
17
+ url = f"https://www.google.com/search?q={query}"
18
+ headers = {"User-Agent": "Mozilla/5.0"}
19
+
20
+ try:
21
+ response = requests.get(url, headers=headers)
22
+ soup = BeautifulSoup(response.text, "html.parser")
23
+ result = soup.find("div", class_="BNeawe iBp4i AP7Wnd")
24
+
25
+ if result:
26
+ return result.text
27
+ return "Sorry, I couldn't find an answer."
28
+ except Exception:
29
+ return "I'm unable to fetch data from Google right now."
30
+
31
+ def get_time_in_city(city):
32
+ """Fetch current time for any city using pytz"""
33
+ try:
34
+ timezone = pytz.timezone(pytz.country_timezones['US'][0]) if city.lower() == "new york" else pytz.utc
35
+ now = datetime.now(timezone)
36
+ return f"The current time in {city} is {now.strftime('%H:%M:%S')}."
37
+ except Exception:
38
+ return "I couldn't fetch the time for that city."
39
+
40
+ def get_current_date():
41
+ """Return today's date"""
42
+ return f"Today's date is {datetime.today().strftime('%d %B %Y')}."
43
+
44
+ def respond(message, history, system_message, max_tokens, temperature, top_p):
45
+ """Chatbot that answers user and fetches real-time info if needed."""
46
+
47
+ message_lower = message.lower()
48
+
49
+ # Time-related questions
50
+ if "what time" in message_lower or "saa ngapi" in message_lower:
51
+ city = message.split()[-1] # Assume last word is city name
52
+ return get_time_in_city(city)
53
+
54
+ # Date-related questions
55
+ if "what date" in message_lower or "leo ni tarehe ngapi" in message_lower:
56
+ return get_current_date()
57
+
58
+ # Weather-related questions
59
+ if "weather" in message_lower or "hali ya hewa" in message_lower:
60
+ city = message.split()[-1]
61
+ return weather.get_weather(city)
62
+
63
+ # General knowledge questions → Try Google if model fails
64
+ messages = [{"role": "system", "content": system_message}]
65
+ for val in history:
66
+ if val[0]: messages.append({"role": "user", "content": val[0]})
67
+ if val[1]: messages.append({"role": "assistant", "content": val[1]})
68
+ messages.append({"role": "user", "content": message})
69
+
70
+ response = ""
71
+ for message in client.chat_completion(messages, max_tokens=max_tokens, stream=True, temperature=temperature, top_p=top_p):
72
+ token = message.choices[0].delta.content
73
+ response += token
74
+
75
+ # If model doesn't know, use Google
76
+ if "I don't know" in response or response.strip() == "":
77
+ response = google_search(message)
78
+
79
+ return response
80
+
81
+ # Gradio UI
82
+ demo = gr.ChatInterface(
83
+ respond,
84
+ additional_inputs=[
85
+ gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
86
+ gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
87
+ gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
88
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.95, step=0.05, label="Top-p"),
89
+ ],
90
+ )
91
+
92
+ if __name__ == "__main__":
93
+ demo.launch()
94
+