Update app.py
Browse files
app.py
CHANGED
|
@@ -9,8 +9,7 @@ import gradio as gr
|
|
| 9 |
from dateutil import parser as dateparser
|
| 10 |
from fastapi import FastAPI
|
| 11 |
from pydantic import BaseModel
|
| 12 |
-
import google.generativeai
|
| 13 |
-
# <<< MISSING IMPORT ADDED BACK >>>
|
| 14 |
from analytics import record_request, last_n_days_df, last_n_days_avg_time_df
|
| 15 |
|
| 16 |
# --- Prompts ---
|
|
@@ -72,11 +71,12 @@ async def search_web_logic(query: str, serper_api_key: str, search_type: str, nu
|
|
| 72 |
except Exception as e:
|
| 73 |
return f"An error occurred during web search: {str(e)}"
|
| 74 |
|
| 75 |
-
# ---
|
| 76 |
-
async def
|
|
|
|
|
|
|
| 77 |
try:
|
| 78 |
-
|
| 79 |
-
model = genai.GenerativeModel(model_name)
|
| 80 |
current_date = datetime.now(timezone.utc).strftime("%Y-%m-%d")
|
| 81 |
|
| 82 |
if research_mode == 'deep':
|
|
@@ -85,18 +85,22 @@ async def summarize_with_gemini(text_to_summarize: str, query: str, gemini_key:
|
|
| 85 |
prompt_template = PROMPT_NORMAL
|
| 86 |
|
| 87 |
prompt = prompt_template.format(query=query, context_text=text_to_summarize, current_date=current_date)
|
| 88 |
-
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
except Exception as e:
|
| 91 |
-
return f"\n\n--- ⚠️
|
| 92 |
|
| 93 |
# --- Main Orchestrator Function ---
|
| 94 |
-
async def search_and_summarize(query, serper_api_key, search_type, num_results,
|
| 95 |
scraped_text = await search_web_logic(query, serper_api_key, search_type, num_results)
|
| 96 |
|
| 97 |
-
if
|
| 98 |
-
summarized_text = await
|
| 99 |
-
if "⚠️
|
| 100 |
return scraped_text + summarized_text
|
| 101 |
else:
|
| 102 |
return summarized_text
|
|
@@ -104,24 +108,21 @@ async def search_and_summarize(query, serper_api_key, search_type, num_results,
|
|
| 104 |
|
| 105 |
# --- FastAPI App ---
|
| 106 |
app = FastAPI()
|
| 107 |
-
# Add CORS middleware if you plan to call the API from a different domain/frontend
|
| 108 |
-
# from fastapi.middleware.cors import CORSMiddleware
|
| 109 |
-
# app.add_middleware(CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=["*"])
|
| 110 |
|
| 111 |
class SearchRequest(BaseModel):
|
| 112 |
query: str
|
| 113 |
serper_api_key: str
|
| 114 |
search_type: str = "search"
|
| 115 |
num_results: int = 4
|
| 116 |
-
|
| 117 |
-
|
| 118 |
research_mode: str = "normal"
|
| 119 |
|
| 120 |
@app.post("/api/search")
|
| 121 |
async def api_search(request: SearchRequest):
|
| 122 |
result = await search_and_summarize(
|
| 123 |
request.query, request.serper_api_key, request.search_type, request.num_results,
|
| 124 |
-
request.
|
| 125 |
)
|
| 126 |
return {"result": result}
|
| 127 |
|
|
@@ -138,16 +139,16 @@ def create_gradio_app():
|
|
| 138 |
search_type_input = gr.Radio(["search", "news"], value="search", label="Search Type")
|
| 139 |
num_results_input = gr.Slider(1, 20, value=4, step=1, label="Number of Results")
|
| 140 |
|
| 141 |
-
gr.Markdown("### Step 2: AI Summarization")
|
| 142 |
research_mode_input = gr.Radio(["normal", "deep"], value="normal", label="Research Mode", info="Normal for fast summary, Deep for detailed report.")
|
| 143 |
-
|
| 144 |
-
|
| 145 |
search_button = gr.Button("Search & Summarize", variant="primary")
|
| 146 |
output = gr.Textbox(label="Result", lines=25, max_lines=40)
|
| 147 |
|
| 148 |
search_button.click(
|
| 149 |
fn=search_and_summarize,
|
| 150 |
-
inputs=[query_input, serper_api_key_input, search_type_input, num_results_input,
|
| 151 |
outputs=output
|
| 152 |
)
|
| 153 |
with gr.Tab("Analytics"):
|
|
|
|
| 9 |
from dateutil import parser as dateparser
|
| 10 |
from fastapi import FastAPI
|
| 11 |
from pydantic import BaseModel
|
| 12 |
+
from groq import AsyncGroq # <<< CHANGED from google.generativeai
|
|
|
|
| 13 |
from analytics import record_request, last_n_days_df, last_n_days_avg_time_df
|
| 14 |
|
| 15 |
# --- Prompts ---
|
|
|
|
| 71 |
except Exception as e:
|
| 72 |
return f"An error occurred during web search: {str(e)}"
|
| 73 |
|
| 74 |
+
# --- Groq Summarization Logic (REPLACED) ---
|
| 75 |
+
async def summarize_with_groq(text_to_summarize: str, query: str, groq_key: str, model_name: str, research_mode: str) -> str:
|
| 76 |
+
if not groq_key:
|
| 77 |
+
return "\n\n--- ⚠️ Groq Summarization Skipped ---\nError: API Key is required.\nReturning raw text instead."
|
| 78 |
try:
|
| 79 |
+
client = AsyncGroq(api_key=groq_key)
|
|
|
|
| 80 |
current_date = datetime.now(timezone.utc).strftime("%Y-%m-%d")
|
| 81 |
|
| 82 |
if research_mode == 'deep':
|
|
|
|
| 85 |
prompt_template = PROMPT_NORMAL
|
| 86 |
|
| 87 |
prompt = prompt_template.format(query=query, context_text=text_to_summarize, current_date=current_date)
|
| 88 |
+
|
| 89 |
+
chat_completion = await client.chat.completions.create(
|
| 90 |
+
messages=[{"role": "user", "content": prompt}],
|
| 91 |
+
model=model_name,
|
| 92 |
+
)
|
| 93 |
+
return chat_completion.choices[0].message.content
|
| 94 |
except Exception as e:
|
| 95 |
+
return f"\n\n--- ⚠️ Groq Summarization Failed ---\nError: {str(e)}\nReturning raw text instead."
|
| 96 |
|
| 97 |
# --- Main Orchestrator Function ---
|
| 98 |
+
async def search_and_summarize(query, serper_api_key, search_type, num_results, groq_api_key, groq_model, research_mode):
|
| 99 |
scraped_text = await search_web_logic(query, serper_api_key, search_type, num_results)
|
| 100 |
|
| 101 |
+
if groq_api_key and "Error:" not in scraped_text:
|
| 102 |
+
summarized_text = await summarize_with_groq(scraped_text, query, groq_api_key, groq_model, research_mode) # CHANGED
|
| 103 |
+
if "⚠️ Groq Summarization Failed" in summarized_text:
|
| 104 |
return scraped_text + summarized_text
|
| 105 |
else:
|
| 106 |
return summarized_text
|
|
|
|
| 108 |
|
| 109 |
# --- FastAPI App ---
|
| 110 |
app = FastAPI()
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
class SearchRequest(BaseModel):
|
| 113 |
query: str
|
| 114 |
serper_api_key: str
|
| 115 |
search_type: str = "search"
|
| 116 |
num_results: int = 4
|
| 117 |
+
groq_api_key: Optional[str] = None # CHANGED
|
| 118 |
+
groq_model: Optional[str] = "llama3-8b-8192" # CHANGED
|
| 119 |
research_mode: str = "normal"
|
| 120 |
|
| 121 |
@app.post("/api/search")
|
| 122 |
async def api_search(request: SearchRequest):
|
| 123 |
result = await search_and_summarize(
|
| 124 |
request.query, request.serper_api_key, request.search_type, request.num_results,
|
| 125 |
+
request.groq_api_key, request.groq_model, request.research_mode
|
| 126 |
)
|
| 127 |
return {"result": result}
|
| 128 |
|
|
|
|
| 139 |
search_type_input = gr.Radio(["search", "news"], value="search", label="Search Type")
|
| 140 |
num_results_input = gr.Slider(1, 20, value=4, step=1, label="Number of Results")
|
| 141 |
|
| 142 |
+
gr.Markdown("### Step 2: AI Summarization (Powered by Groq)") # CHANGED
|
| 143 |
research_mode_input = gr.Radio(["normal", "deep"], value="normal", label="Research Mode", info="Normal for fast summary, Deep for detailed report.")
|
| 144 |
+
groq_api_key_input = gr.Textbox(label="Your Groq API Key", type="password", placeholder="Leave empty to skip summarization") # CHANGED
|
| 145 |
+
groq_model_input = gr.Textbox(label="Groq Model", value="llama3-8b-8192") # CHANGED
|
| 146 |
search_button = gr.Button("Search & Summarize", variant="primary")
|
| 147 |
output = gr.Textbox(label="Result", lines=25, max_lines=40)
|
| 148 |
|
| 149 |
search_button.click(
|
| 150 |
fn=search_and_summarize,
|
| 151 |
+
inputs=[query_input, serper_api_key_input, search_type_input, num_results_input, groq_api_key_input, groq_model_input, research_mode_input], # CHANGED
|
| 152 |
outputs=output
|
| 153 |
)
|
| 154 |
with gr.Tab("Analytics"):
|