File size: 6,377 Bytes
0ed2698
b5d2612
602e30b
4efbe1d
4ee7feb
 
 
 
4efbe1d
570a345
b5d2612
48c06e2
 
 
 
 
 
 
 
 
 
 
 
f5d48e1
b5d2612
f5d48e1
 
 
 
b5d2612
53b8fb0
f5d48e1
b5d2612
48c06e2
 
 
10707fd
48c06e2
10707fd
 
 
 
 
48c06e2
10707fd
48c06e2
10707fd
48c06e2
f0e3a92
4ee7feb
 
 
48c06e2
 
f5d48e1
e13157e
48c06e2
f5d48e1
 
ab836a5
 
e13157e
3d9b407
 
f5d48e1
 
48c06e2
f5d48e1
e13157e
3d6bd10
e13157e
 
 
 
e19ec1b
48c06e2
 
f5d48e1
570a345
4ee7feb
48c06e2
f5d48e1
4ee7feb
 
f5d48e1
570a345
48c06e2
f5d48e1
 
48c06e2
f5d48e1
 
570a345
f5d48e1
570a345
e13157e
48c06e2
4ee7feb
f5d48e1
e8cb689
f5d48e1
ab836a5
48c06e2
3d6bd10
 
 
 
e743109
3d6bd10
a10fcf7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5d48e1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3d6bd10
f5d48e1
0ed2698
48c06e2
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
import gradio as gr
from duckduckgo_search import DDGS
from typing import List, Dict
import os
import logging

logging.basicConfig(level=logging.INFO)

# Environment variables and configurations
huggingface_token = os.environ.get("HUGGINGFACE_TOKEN")

class ConversationManager:
    def __init__(self):
        self.history = []
        self.current_context = None

    def add_interaction(self, query, response):
        self.history.append((query, response))
        self.current_context = f"Previous query: {query}\nPrevious response summary: {response[:200]}..."

    def get_context(self):
        return self.current_context

def get_web_search_results(query: str, max_results: int = 10) -> List[Dict[str, str]]:
    try:
        results = list(DDGS().text(query, max_results=max_results))
        if not results:
            print(f"No results found for query: {query}")
        return results
    except Exception as e:
        print(f"An error occurred during web search: {str(e)}")
        return [{"error": f"An error occurred during web search: {str(e)}"}]

def rephrase_query(original_query: str, conversation_manager: ConversationManager) -> str:
    context = conversation_manager.get_context()
    if context:
        prompt = f"""You are a highly intelligent conversational chatbot. Your task is to analyze the given context and new query, then decide whether to rephrase the query with or without incorporating the context. Follow these steps:

        1. Determine if the new query is a continuation of the previous conversation or an entirely new topic.
        2. If it's a continuation, rephrase the query by incorporating relevant information from the context to make it more specific and contextual.
        3. If it's a new topic, rephrase the query to make it more appropriate for a web search, focusing on clarity and accuracy without using the previous context.
        4. Provide ONLY the rephrased query without any additional explanation or reasoning.
        
        Context: {context}
        
        New query: {original_query}
        
        Rephrased query:"""
        response = DDGS().chat(prompt, model="llama-3.1-70b")
        # Extract only the rephrased query, removing any explanations
        rephrased_query = response.split('\n')[0].strip()
        return rephrased_query
    return original_query

def summarize_results(query: str, search_results: List[Dict[str, str]], conversation_manager: ConversationManager) -> str:
    try:
        context = conversation_manager.get_context()
        search_context = "\n\n".join([f"Title: {result['title']}\nContent: {result['body']}" for result in search_results])

        prompt = f"""You are a highly intelligent & expert analyst and your job is to skillfully articulate the web search results about '{query}' and considering the context: {context}, 
        You have to create a comprehensive news summary FOCUSING on the context provided to you. 
        Include key facts, relevant statistics, and expert opinions if available. 
        Ensure the article is well-structured with an introduction, main body, and conclusion, IF NECESSARY. 
        Address the query in the context of the ongoing conversation IF APPLICABLE.
        Cite sources directly within the generated text and not at the end of the generated text, integrating URLs where appropriate to support the information provided:

        {search_context}

        Article:"""

        summary = DDGS().chat(prompt, model="llama-3-70b")
        return summary
    except Exception as e:
        return f"An error occurred during summarization: {str(e)}"

conversation_manager = ConversationManager()

def respond(message, chat_history, temperature, num_api_calls):
    final_summary = ""
    original_query = message
    rephrased_query = rephrase_query(message, conversation_manager)

    logging.info(f"Original query: {original_query}")
    logging.info(f"Rephrased query: {rephrased_query}")

    for _ in range(num_api_calls):
        search_results = get_web_search_results(rephrased_query)

        if not search_results:
            final_summary += f"No search results found for the query: {rephrased_query}\n\n"
        elif "error" in search_results[0]:
            final_summary += search_results[0]["error"] + "\n\n"
        else:
            summary = summarize_results(rephrased_query, search_results, conversation_manager)
            final_summary += summary + "\n\n"

    if final_summary:
        conversation_manager.add_interaction(original_query, final_summary)
        return final_summary
    else:
        return "Unable to generate a response. Please try a different query."

# The rest of your code (CSS, theme, and Gradio interface setup) remains the same
css = """
Your custom CSS here
"""

custom_placeholder = "Ask me anything about web content"

theme = gr.themes.Soft(
    primary_hue="orange",
    secondary_hue="amber",
    neutral_hue="gray",
    font=[gr.themes.GoogleFont("Exo"), "ui-sans-serif", "system-ui", "sans-serif"]
).set(
    body_background_fill_dark="#0c0505",
    block_background_fill_dark="#0c0505",
    block_border_width="1px",
    block_title_background_fill_dark="#1b0f0f",
    input_background_fill_dark="#140b0b",
    button_secondary_background_fill_dark="#140b0b",
    border_color_accent_dark="#1b0f0f",
    border_color_primary_dark="#1b0f0f",
    background_fill_secondary_dark="#0c0505",
    color_accent_soft_dark="transparent",
    code_background_fill_dark="#140b0b"
)

demo = gr.ChatInterface(
    respond,
    additional_inputs=[
        gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature"),
        gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls")
    ],
    title="AI-powered Web Search and PDF Chat Assistant",
    description="This AI-powered Web Search and PDF Chat Assistant combines real-time web search capabilities with advanced language processing.",
    theme=theme,
    css=css,
    examples=[
        ["What is AI"],
        ["Any recent news on US Banks"],
        ["Who is Donald Trump"]
    ],
    cache_examples=False,
    analytics_enabled=False,
    textbox=gr.Textbox(placeholder=custom_placeholder, container=False, scale=7),
    chatbot=gr.Chatbot(
        show_copy_button=True,
        likeable=True,
        layout="bubble",
        height=400,
    )
)

demo.launch()