itsanurag commited on
Commit
74904d2
·
verified ·
1 Parent(s): 7f861b2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -19
app.py CHANGED
@@ -1,8 +1,10 @@
1
  from huggingface_hub import InferenceClient
2
  import gradio as gr
3
- from langchain_community.tools import DuckDuckGoSearchRun
4
 
5
  client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
 
 
6
  duckduckgo_search = DuckDuckGoSearchRun()
7
 
8
  def format_prompt(message, history):
@@ -29,22 +31,24 @@ def generate(prompt, history, system_prompt, temperature=0.9, max_new_tokens=256
29
  )
30
 
31
  formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
 
 
32
  stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
 
33
 
34
  for response in stream:
35
- yield response.token.text
36
-
37
- def search_and_generate(query, prompt, history, system_prompt, temperature=0.9, max_new_tokens=256, top_p=0.95, repetition_penalty=1.0):
38
- # Search for the query using DuckDuckGo
39
- search_results = duckduckgo_search.run(query)
40
- if search_results:
41
- # If search results are found, use them as prompt
42
- prompt = search_results[0] # Assuming the first search result is the most relevant
43
- # Generate response
44
- for generated_response in generate(prompt, history, system_prompt, temperature, max_new_tokens, top_p, repetition_penalty):
45
- yield generated_response
46
 
47
- additional_inputs=[
48
  gr.Textbox(
49
  label="System Prompt",
50
  max_lines=1,
@@ -88,15 +92,15 @@ additional_inputs=[
88
  )
89
  ]
90
 
91
- examples=[
92
- ["I'm planning a vacation to Japan. Can you suggest a one-week itinerary including must-visit places and local cuisines to try?", None, None, None, None, None, ],
93
- ["What are some best tourist places to visit in Lajpat nagar, Delhi?", None, None, None, None, None,],
94
- ["Ronaldo or Messi?", None, None, None, None, None,],
95
- ["Can you explain how the QuickSort algorithm works and provide a Python implementation?", None, None, None, None, None,],
96
  ]
97
 
98
  gr.ChatInterface(
99
- fn=search_and_generate,
100
  chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel"),
101
  additional_inputs=additional_inputs,
102
  title="🤗 friday2.0 🤗 WELCOME TO OPEN-SOURCE FREEDOM",
 
1
  from huggingface_hub import InferenceClient
2
  import gradio as gr
3
+ from langchain.tools import DuckDuckGoSearchRun
4
 
5
  client = InferenceClient("mistralai/Mixtral-8x7B-Instruct-v0.1")
6
+
7
+ # Initialize DuckDuckGo search tool
8
  duckduckgo_search = DuckDuckGoSearchRun()
9
 
10
  def format_prompt(message, history):
 
31
  )
32
 
33
  formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
34
+
35
+ # Generate response using model
36
  stream = client.text_generation(formatted_prompt, **generate_kwargs, stream=True, details=True, return_full_text=False)
37
+ output = ""
38
 
39
  for response in stream:
40
+ output += response.token.text
41
+ # Yield model's response first
42
+ yield output
43
+
44
+ # Now, perform DuckDuckGo search and yield results
45
+ search_result = duckduckgo_search.run(prompt)
46
+ if search_result:
47
+ yield search_result
48
+ else:
49
+ yield "Sorry, I couldn't find any relevant information."
 
50
 
51
+ additional_inputs = [
52
  gr.Textbox(
53
  label="System Prompt",
54
  max_lines=1,
 
92
  )
93
  ]
94
 
95
+ examples = [
96
+ ["I'm planning a vacation to Japan. Can you suggest a one-week itinerary including must-visit places and local cuisines to try?", None, None, None, None, None],
97
+ ["What are some best tourist places to visit in Lajpat nagar, Delhi?", None, None, None, None, None],
98
+ ["Ronaldo or Messi?", None, None, None, None, None],
99
+ ["Can you explain how the QuickSort algorithm works and provide a Python implementation?", None, None, None, None, None],
100
  ]
101
 
102
  gr.ChatInterface(
103
+ fn=generate,
104
  chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel"),
105
  additional_inputs=additional_inputs,
106
  title="🤗 friday2.0 🤗 WELCOME TO OPEN-SOURCE FREEDOM",