Spaces:
				
			
			
	
			
			
					
		Running
		
	
	
	
			
			
	
	
	
	
		
		
					
		Running
		
	Update app.py
Browse files
    	
        app.py
    CHANGED
    
    | @@ -27,12 +27,12 @@ def fetch_search_results(query): | |
| 27 |  | 
| 28 | 
             
                try:
         | 
| 29 | 
             
                    response = client.chat.completions.create(
         | 
| 30 | 
            -
                        model="gemini-2.0-flash-lite",  # Adjust model name as needed
         | 
| 31 | 
             
                        messages=[
         | 
| 32 | 
             
                            {"role": "system", "content": "You are a helpful search engine."},
         | 
| 33 | 
             
                            {"role": "user", "content": prompt}
         | 
| 34 | 
             
                        ],
         | 
| 35 | 
            -
                        response_format={"type": "json_object"} | 
| 36 | 
             
                    )
         | 
| 37 |  | 
| 38 | 
             
                    content = response.choices[0].message.content
         | 
| @@ -49,7 +49,13 @@ def fetch_search_results(query): | |
| 49 | 
             
                    return results, None
         | 
| 50 |  | 
| 51 | 
             
                except Exception as e:
         | 
| 52 | 
            -
                     | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 53 |  | 
| 54 | 
             
            def display_search_results(query, page=1):
         | 
| 55 | 
             
                """Display search results for the given query and page number."""
         | 
| @@ -98,7 +104,7 @@ def display_search_results(query, page=1): | |
| 98 | 
             
                    html += f'<li class="search-result"><h3>{title}</h3><p>{snippet}</p></li>'
         | 
| 99 | 
             
                html += "</ul>"
         | 
| 100 |  | 
| 101 | 
            -
                # Add pagination controls | 
| 102 | 
             
                html += '<div class="pagination">'
         | 
| 103 | 
             
                if page > 1:
         | 
| 104 | 
             
                    html += f'<button onclick="update_page({page - 1})">Previous</button>'
         | 
| @@ -116,7 +122,7 @@ def search_handler(query, page): | |
| 116 | 
             
            # Build Gradio interface with Blocks for state management
         | 
| 117 | 
             
            with gr.Blocks(title="LLM Search Engine") as app:
         | 
| 118 | 
             
                gr.Markdown("# LLM Search Engine")
         | 
| 119 | 
            -
                gr.Markdown("Enter a query below to search using a large language model.")
         | 
| 120 |  | 
| 121 | 
             
                query_input = gr.Textbox(label="Search Query", placeholder="Type your search here...")
         | 
| 122 | 
             
                search_button = gr.Button("Search")
         | 
| @@ -129,6 +135,12 @@ with gr.Blocks(title="LLM Search Engine") as app: | |
| 129 | 
             
                def on_submit(query, page):
         | 
| 130 | 
             
                    return search_handler(query, page), page
         | 
| 131 |  | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 132 | 
             
                search_button.click(
         | 
| 133 | 
             
                    fn=on_submit,
         | 
| 134 | 
             
                    inputs=[query_input, page_state],
         | 
| @@ -158,8 +170,17 @@ with gr.Blocks(title="LLM Search Engine") as app: | |
| 158 | 
             
                )
         | 
| 159 |  | 
| 160 | 
             
                # Update button visibility after search
         | 
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
|  | |
| 161 | 
             
                search_button.click(
         | 
| 162 | 
            -
                    fn= | 
| 163 | 
             
                    inputs=[query_input, page_state],
         | 
| 164 | 
             
                    outputs=[output_html, page_state, prev_button, next_button]
         | 
| 165 | 
             
                )
         | 
|  | |
| 27 |  | 
| 28 | 
             
                try:
         | 
| 29 | 
             
                    response = client.chat.completions.create(
         | 
| 30 | 
            +
                        model="gemini-2.0-flash-lite",  # Adjust model name as needed (e.g., 'xai-model-name')
         | 
| 31 | 
             
                        messages=[
         | 
| 32 | 
             
                            {"role": "system", "content": "You are a helpful search engine."},
         | 
| 33 | 
             
                            {"role": "user", "content": prompt}
         | 
| 34 | 
             
                        ],
         | 
| 35 | 
            +
                        response_format={"type": "json_object"}
         | 
| 36 | 
             
                    )
         | 
| 37 |  | 
| 38 | 
             
                    content = response.choices[0].message.content
         | 
|  | |
| 49 | 
             
                    return results, None
         | 
| 50 |  | 
| 51 | 
             
                except Exception as e:
         | 
| 52 | 
            +
                    error_msg = str(e)
         | 
| 53 | 
            +
                    if "404" in error_msg:
         | 
| 54 | 
            +
                        return None, f"Error 404: Model or endpoint not found. Check OPENAI_BASE_URL ({os.environ['OPENAI_BASE_URL']}) and model name."
         | 
| 55 | 
            +
                    elif "401" in error_msg:
         | 
| 56 | 
            +
                        return None, "Error 401: Invalid API key. Check OPENAI_API_KEY."
         | 
| 57 | 
            +
                    else:
         | 
| 58 | 
            +
                        return None, f"Error: {error_msg}"
         | 
| 59 |  | 
| 60 | 
             
            def display_search_results(query, page=1):
         | 
| 61 | 
             
                """Display search results for the given query and page number."""
         | 
|  | |
| 104 | 
             
                    html += f'<li class="search-result"><h3>{title}</h3><p>{snippet}</p></li>'
         | 
| 105 | 
             
                html += "</ul>"
         | 
| 106 |  | 
| 107 | 
            +
                # Add pagination controls
         | 
| 108 | 
             
                html += '<div class="pagination">'
         | 
| 109 | 
             
                if page > 1:
         | 
| 110 | 
             
                    html += f'<button onclick="update_page({page - 1})">Previous</button>'
         | 
|  | |
| 122 | 
             
            # Build Gradio interface with Blocks for state management
         | 
| 123 | 
             
            with gr.Blocks(title="LLM Search Engine") as app:
         | 
| 124 | 
             
                gr.Markdown("# LLM Search Engine")
         | 
| 125 | 
            +
                gr.Markdown("Enter a query below to search using a large language model (press Enter or click Search).")
         | 
| 126 |  | 
| 127 | 
             
                query_input = gr.Textbox(label="Search Query", placeholder="Type your search here...")
         | 
| 128 | 
             
                search_button = gr.Button("Search")
         | 
|  | |
| 135 | 
             
                def on_submit(query, page):
         | 
| 136 | 
             
                    return search_handler(query, page), page
         | 
| 137 |  | 
| 138 | 
            +
                # Trigger search on Enter key or button click
         | 
| 139 | 
            +
                query_input.submit(
         | 
| 140 | 
            +
                    fn=on_submit,
         | 
| 141 | 
            +
                    inputs=[query_input, page_state],
         | 
| 142 | 
            +
                    outputs=[output_html, page_state]
         | 
| 143 | 
            +
                )
         | 
| 144 | 
             
                search_button.click(
         | 
| 145 | 
             
                    fn=on_submit,
         | 
| 146 | 
             
                    inputs=[query_input, page_state],
         | 
|  | |
| 170 | 
             
                )
         | 
| 171 |  | 
| 172 | 
             
                # Update button visibility after search
         | 
| 173 | 
            +
                def update_visibility(query, page):
         | 
| 174 | 
            +
                    html, prev_page, next_page = display_search_results(query, page)
         | 
| 175 | 
            +
                    return html, page, gr.update(visible=prev_page is not None), gr.update(visible=next_page is not None)
         | 
| 176 | 
            +
                
         | 
| 177 | 
            +
                query_input.submit(
         | 
| 178 | 
            +
                    fn=update_visibility,
         | 
| 179 | 
            +
                    inputs=[query_input, page_state],
         | 
| 180 | 
            +
                    outputs=[output_html, page_state, prev_button, next_button]
         | 
| 181 | 
            +
                )
         | 
| 182 | 
             
                search_button.click(
         | 
| 183 | 
            +
                    fn=update_visibility,
         | 
| 184 | 
             
                    inputs=[query_input, page_state],
         | 
| 185 | 
             
                    outputs=[output_html, page_state, prev_button, next_button]
         | 
| 186 | 
             
                )
         | 
