File size: 4,416 Bytes
2a20c6b
 
a9c7e25
cd2ca57
892e0a4
2a20c6b
a9c7e25
 
 
cd2ca57
2a20c6b
a9c7e25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0d944ef
a9c7e25
0d944ef
 
a9c7e25
 
 
 
 
 
0d944ef
 
892e0a4
2a20c6b
a9c7e25
2a20c6b
a9c7e25
 
 
2a20c6b
a9c7e25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
892e0a4
 
a9c7e25
188ef10
a9c7e25
 
 
 
 
 
 
 
892e0a4
a9c7e25
 
892e0a4
2a20c6b
 
a9c7e25
 
 
2a20c6b
a9c7e25
2a20c6b
 
 
 
a9c7e25
2a20c6b
a9c7e25
 
2a20c6b
5426913
a9c7e25
 
 
 
70cac6e
 
 
 
 
 
 
 
 
 
 
 
a9c7e25
01cb892
2a20c6b
6982b56
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import gradio as gr
from huggingface_hub import InferenceClient
import pymupdf
from duckduckgo_search import DDGS
from serpapi import GoogleSearch

"""
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
"""
client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")


# PDF Parsing
def extract_text_from_pdf(pdf_file):
    doc = pymupdf.open(pdf_file)
    text = " ".join([page.get_textpage().extractTEXT() for page in doc])
    return text


# Web search fallback
def search_web(query):
    with DDGS() as ddgs:
        results = ddgs.text(query)
        if results:
            return results[0]["body"]
    return "No relevant results found on the web."


def google_search(query):
    params = {
        "q": query,
        "api_key": "b11d4a3660600e7e7f481b3288f107fbf993389a20125b0a97ebe7ab207854a5",  # Replace this with your real key
        "engine": "google",
    }
    search = GoogleSearch(params)
    results = search.get_dict()
    if "organic_results" in results:
        # Combine top 3 results
        summaries = []
        for res in results["organic_results"][:3]:
            title = res.get("title", "")
            snippet = res.get("snippet", "")
            summaries.append(f"{title}: {snippet}")
        return "\n".join(summaries)
    return None


SYSTEM_PROMPT = """
You are an intelligent and friendly AI assistant. 

Your goals:
- Answer user questions clearly and concisely.
- If a PDF document is provided, use its content to give informed answers.
- For questions about recent or live topics (e.g., news, prices, events), you may perform a web search and summarize the result.
- If no document or web context is available, still try to help using general knowledge.
- Be honest if you don’t know something.
- Always be polite, helpful, and respectful.
"""


def respond(
    message,
    history: list[tuple[str, str]],
    max_tokens=2048,
    temperature=0.4,
    top_p=0.1,
):

    recent_keywords = [
        "latest",
        "today",
        "current",
        "now",
        "recent",
        "news",
        "update",
        "price",
        "who won",
        "what happened",
        "trending",
        "breaking",
        "just in",
        "new release",
        "live",
        "score",
        "results",
        "weather",
        "forecast",
        "report",
        "market",
        "stocks",
        "crypto",
        "rate",
        "exchange",
        "gold price",
        "happening",
        "event",
        "updates",
        "hot",
        "viral",
        "announcement",
        "today's",
        "this week",
        "schedule",
        "calendar",
        "launch",
        "drop",
        "release date",
        "opening",
        "closing",
        "deadline",
    ]

    message_lower = message.lower()

    messages = [{"role": "system", "content": SYSTEM_PROMPT}]

    for val in history:
        if val[0]:
            messages.append({"role": "user", "content": val[0]})
        if val[1]:
            messages.append({"role": "assistant", "content": val[1]})

    if any(kw in message_lower for kw in recent_keywords):
        web_context = google_search(message)
        if web_context:
            # Inject web context as part of the user's query
            message = f"{message}\n\n[Relevant web search results to help you answer]:\n{web_context}"

    messages.append({"role": "user", "content": message})

    response = ""

    for message in client.chat_completion(
        messages,
        max_tokens=max_tokens,
        stream=True,
        temperature=temperature,
        top_p=top_p,
    ):
        token = message.choices[0].delta.content

        response += token
        yield response


"""
For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
"""
demo = gr.ChatInterface(
    respond
    # additional_inputs=[
    #     gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
    #     gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
    #     gr.Slider(
    #         minimum=0.1,
    #         maximum=1.0,
    #         value=0.95,
    #         step=0.05,
    #         label="Top-p (nucleus sampling)",
    #     ),
    # ],
)

if __name__ == "__main__":
    demo.launch()