Shreyas094 commited on
Commit
3d6bd10
·
verified ·
1 Parent(s): 6fa24f7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +77 -28
app.py CHANGED
@@ -7,6 +7,13 @@ import os
7
  # Environment variables and configurations
8
  huggingface_token = os.environ.get("HUGGINGFACE_TOKEN")
9
 
 
 
 
 
 
 
 
10
  def get_web_search_results(query: str, max_results: int = 10) -> List[Dict[str, str]]:
11
  try:
12
  results = DDGS().text(query, max_results=max_results)
@@ -14,7 +21,7 @@ def get_web_search_results(query: str, max_results: int = 10) -> List[Dict[str,
14
  except Exception as e:
15
  return [{"error": f"An error occurred during web search: {str(e)}"}]
16
 
17
- def summarize_results(query: str, search_results: List[Dict[str, str]]) -> str:
18
  try:
19
  context = "\n\n".join([f"Title: {result['title']}\nContent: {result['body']}" for result in search_results])
20
 
@@ -27,7 +34,7 @@ def summarize_results(query: str, search_results: List[Dict[str, str]]) -> str:
27
 
28
  Article:"""
29
 
30
- summary = DDGS().chat(prompt, model='llama-3-70b')
31
  return summary
32
  except Exception as e:
33
  return f"An error occurred during summarization: {str(e)}"
@@ -51,35 +58,77 @@ def rephrase_query(previous_response: str, new_query: str) -> str:
51
  response = client.text_generation(prompt, max_new_tokens=100, temperature=0.7)
52
  return response
53
 
54
- def web_search_and_summarize(input_text: str, chat_history: List[Tuple[str, str]]) -> Tuple[str, List[Tuple[str, str]]]:
 
55
  if chat_history:
56
  previous_response = chat_history[-1][1]
57
- rephrased_query = rephrase_query(previous_response, input_text)
58
- else:
59
- rephrased_query = input_text
60
-
61
- search_results = get_web_search_results(rephrased_query)
62
- if "error" in search_results[0]:
63
- summary = search_results[0]["error"]
64
- else:
65
- summary = summarize_results(rephrased_query, search_results)
66
-
67
- chat_history.append((input_text, summary))
68
- return summary, chat_history
69
-
70
- iface = gr.Interface(
71
- fn=web_search_and_summarize,
72
- inputs=[
73
- gr.Textbox(lines=2, placeholder="Enter your query here..."),
74
- gr.State([])
 
 
 
 
 
 
 
 
 
 
 
 
75
  ],
76
- outputs=[
77
- gr.Textbox(label="Response"),
78
- gr.State()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  ],
80
- title="DuckDuckGo Web Search and Summary",
81
- description="Enter a query to get a summary based on web search results using DuckDuckGo.",
82
- allow_flagging="never"
 
 
 
 
 
 
 
83
  )
84
 
85
- iface.launch()
 
7
  # Environment variables and configurations
8
  huggingface_token = os.environ.get("HUGGINGFACE_TOKEN")
9
 
10
+ MODELS = [
11
+ "mistralai/Mistral-7B-Instruct-v0.3",
12
+ "mistralai/Mixtral-8x7B-Instruct-v0.1",
13
+ "mistralai/Mistral-Nemo-Instruct-2407",
14
+ "llama-3-70b"
15
+ ]
16
+
17
  def get_web_search_results(query: str, max_results: int = 10) -> List[Dict[str, str]]:
18
  try:
19
  results = DDGS().text(query, max_results=max_results)
 
21
  except Exception as e:
22
  return [{"error": f"An error occurred during web search: {str(e)}"}]
23
 
24
+ def summarize_results(query: str, search_results: List[Dict[str, str]], model: str) -> str:
25
  try:
26
  context = "\n\n".join([f"Title: {result['title']}\nContent: {result['body']}" for result in search_results])
27
 
 
34
 
35
  Article:"""
36
 
37
+ summary = DDGS().chat(prompt, model="llama-3-70b")
38
  return summary
39
  except Exception as e:
40
  return f"An error occurred during summarization: {str(e)}"
 
58
  response = client.text_generation(prompt, max_new_tokens=100, temperature=0.7)
59
  return response
60
 
61
+ def respond(message, chat_history, model, temperature, num_api_calls):
62
+ rephrased_query = message
63
  if chat_history:
64
  previous_response = chat_history[-1][1]
65
+ rephrased_query = rephrase_query(previous_response, message)
66
+
67
+ final_summary = ""
68
+ for _ in range(num_api_calls):
69
+ search_results = get_web_search_results(rephrased_query)
70
+ if "error" in search_results[0]:
71
+ final_summary += search_results[0]["error"] + "\n"
72
+ else:
73
+ summary = summarize_results(rephrased_query, search_results, model)
74
+ final_summary += summary + "\n\n"
75
+
76
+ return final_summary
77
+
78
+ def initial_conversation():
79
+ return [
80
+ ("Hello! How can I assist you today?", "Welcome! I'm your AI-powered Web Search and PDF Chat Assistant. I can help you find information on the web, summarize content, and analyze PDF documents. What would you like to know?")
81
+ ]
82
+
83
+ css = """
84
+ Your custom CSS here
85
+ """
86
+
87
+ custom_placeholder = "Ask me anything about web content or uploaded PDFs..."
88
+
89
+ demo = gr.ChatInterface(
90
+ respond,
91
+ additional_inputs=[
92
+ gr.Dropdown(choices=MODELS, label="Select Model", value=MODELS[3]),
93
+ gr.Slider(minimum=0.1, maximum=1.0, value=0.2, step=0.1, label="Temperature"),
94
+ gr.Slider(minimum=1, maximum=5, value=1, step=1, label="Number of API Calls")
95
  ],
96
+ title="AI-powered Web Search and PDF Chat Assistant",
97
+ description="This AI-powered Web Search and PDF Chat Assistant combines real-time web search capabilities with advanced language processing. It can analyze web content and PDF documents, providing detailed summaries and engaging in in-depth conversations about the information it discovers. Whether you're researching a topic, seeking current news, or exploring document contents, this tool offers intelligent, context-aware responses to your queries.",
98
+ theme=gr.themes.Soft(
99
+ primary_hue="orange",
100
+ secondary_hue="amber",
101
+ neutral_hue="gray",
102
+ font=[gr.themes.GoogleFont("Exo"), "ui-sans-serif", "system-ui", "sans-serif"]
103
+ ).set(
104
+ body_background_fill_dark="#0c0505",
105
+ block_background_fill_dark="#0c0505",
106
+ block_border_width="1px",
107
+ block_title_background_fill_dark="#1b0f0f",
108
+ input_background_fill_dark="#140b0b",
109
+ button_secondary_background_fill_dark="#140b0b",
110
+ border_color_accent_dark="#1b0f0f",
111
+ border_color_primary_dark="#1b0f0f",
112
+ background_fill_secondary_dark="#0c0505",
113
+ color_accent_soft_dark="transparent",
114
+ code_background_fill_dark="#140b0b"
115
+ ),
116
+ css=css,
117
+ examples=[
118
+ ["Tell me about the contents of the uploaded PDFs."],
119
+ ["What are the main topics discussed in the documents?"],
120
+ ["Can you summarize the key points from the PDFs?"]
121
  ],
122
+ cache_examples=False,
123
+ analytics_enabled=False,
124
+ textbox=gr.Textbox(placeholder=custom_placeholder, container=False, scale=7),
125
+ chatbot = gr.Chatbot(
126
+ show_copy_button=True,
127
+ likeable=True,
128
+ layout="bubble",
129
+ height=400,
130
+ value=initial_conversation()
131
+ )
132
  )
133
 
134
+ demo.launch()