Spaces:
Sleeping
Sleeping
Merge branch 'main' of https://huggingface.co/spaces/pendrag/fenybot-ir
Browse files- app.py +22 -6
- requirements.txt +2 -1
app.py
CHANGED
|
@@ -1,4 +1,6 @@
|
|
| 1 |
from openai import OpenAI
|
|
|
|
|
|
|
| 2 |
import os
|
| 3 |
import requests
|
| 4 |
import json
|
|
@@ -122,7 +124,10 @@ def llm_expand_query(query):
|
|
| 122 |
presence_penalty=0
|
| 123 |
)
|
| 124 |
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
def llm_generate_answer(prompt):
|
| 128 |
""" Generate a response from the LLM """
|
|
@@ -168,7 +173,11 @@ def llm_generate_answer(prompt):
|
|
| 168 |
presence_penalty=0
|
| 169 |
)
|
| 170 |
|
| 171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
def clean_refs(answer, results):
|
| 174 |
""" Clean the references from the answer """
|
|
@@ -202,9 +211,9 @@ def clean_refs(answer, results):
|
|
| 202 |
def search(query, progress=gr.Progress()):
|
| 203 |
time.sleep(1)
|
| 204 |
progress(0, desc="Expanding query...")
|
| 205 |
-
|
| 206 |
progress(0.25, desc="Searching INSPIRE HEP...")
|
| 207 |
-
results = search_inspire(
|
| 208 |
progress(0.50, desc="Generating answer...")
|
| 209 |
context = results_context(results)
|
| 210 |
prompt = user_prompt(query, context)
|
|
@@ -217,21 +226,28 @@ def search(query, progress=gr.Progress()):
|
|
| 217 |
|
| 218 |
# ----------- MAIN ------------------------------------------------------------
|
| 219 |
|
| 220 |
-
|
|
|
|
|
|
|
|
|
|
| 221 |
|
| 222 |
with gr.Blocks() as demo:
|
| 223 |
gr.Markdown("# Feynbot on INSPIRE HEP Search")
|
| 224 |
gr.Markdown("""Specialized academic search tool that combines traditional
|
| 225 |
database searching with AI-powered query expansion and result
|
| 226 |
-
synthesis, focused on
|
| 227 |
with gr.Row():
|
| 228 |
with gr.Column():
|
| 229 |
query = gr.Textbox(label="Search Query")
|
| 230 |
search_btn = gr.Button("Search")
|
| 231 |
examples = gr.Examples([["Which one is closest star?"], ["In which particles does the Higgs Boson decay to?"]], query)
|
|
|
|
|
|
|
|
|
|
| 232 |
with gr.Column():
|
| 233 |
results = gr.Markdown("Answer will appear here...", label="Search Results", )
|
| 234 |
search_btn.click(fn=search, inputs=query, outputs=results, api_name="search", show_progress=True)
|
|
|
|
| 235 |
|
| 236 |
|
| 237 |
demo.launch()
|
|
|
|
| 1 |
from openai import OpenAI
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
+
|
| 4 |
import os
|
| 5 |
import requests
|
| 6 |
import json
|
|
|
|
| 124 |
presence_penalty=0
|
| 125 |
)
|
| 126 |
|
| 127 |
+
return response.choices[0].message.content
|
| 128 |
+
else:
|
| 129 |
+
response = genai.GenerativeModel("gemini-1.5-flash").generate_content(prompt)
|
| 130 |
+
return response.text
|
| 131 |
|
| 132 |
def llm_generate_answer(prompt):
|
| 133 |
""" Generate a response from the LLM """
|
|
|
|
| 173 |
presence_penalty=0
|
| 174 |
)
|
| 175 |
|
| 176 |
+
return response.choices[0].message.content
|
| 177 |
+
|
| 178 |
+
else:
|
| 179 |
+
response = genai.GenerativeModel("gemini-1.5-flash").generate_content(system_desc + "\n\n" + prompt)
|
| 180 |
+
return response.text
|
| 181 |
|
| 182 |
def clean_refs(answer, results):
|
| 183 |
""" Clean the references from the answer """
|
|
|
|
| 211 |
def search(query, progress=gr.Progress()):
|
| 212 |
time.sleep(1)
|
| 213 |
progress(0, desc="Expanding query...")
|
| 214 |
+
expanded_query = llm_expand_query(query)
|
| 215 |
progress(0.25, desc="Searching INSPIRE HEP...")
|
| 216 |
+
results = search_inspire(expanded_query)
|
| 217 |
progress(0.50, desc="Generating answer...")
|
| 218 |
context = results_context(results)
|
| 219 |
prompt = user_prompt(query, context)
|
|
|
|
| 226 |
|
| 227 |
# ----------- MAIN ------------------------------------------------------------
|
| 228 |
|
| 229 |
+
if GENAI_API == "openai":
|
| 230 |
+
client = OpenAI()
|
| 231 |
+
else:
|
| 232 |
+
genai.configure(api_key=os.getenv('GEMINI_API_KEY'))
|
| 233 |
|
| 234 |
with gr.Blocks() as demo:
|
| 235 |
gr.Markdown("# Feynbot on INSPIRE HEP Search")
|
| 236 |
gr.Markdown("""Specialized academic search tool that combines traditional
|
| 237 |
database searching with AI-powered query expansion and result
|
| 238 |
+
synthesis, focused on High Energy Physics research papers.""")
|
| 239 |
with gr.Row():
|
| 240 |
with gr.Column():
|
| 241 |
query = gr.Textbox(label="Search Query")
|
| 242 |
search_btn = gr.Button("Search")
|
| 243 |
examples = gr.Examples([["Which one is closest star?"], ["In which particles does the Higgs Boson decay to?"]], query)
|
| 244 |
+
with gr.Row():
|
| 245 |
+
gr.HTML("<a href='https://sinai.ujaen.es'><img src='https://sinai.ujaen.es/sites/default/files/SINAI%20-%20logo%20tx%20azul%20%5Baf%5D.png' width='200'></img></a>")
|
| 246 |
+
gr.HTML("<a href='https://www.ujaen.es'><img src='https://diariodigital.ujaen.es/sites/default/files/general/logo-uja.svg' width='180'></img></a>")
|
| 247 |
with gr.Column():
|
| 248 |
results = gr.Markdown("Answer will appear here...", label="Search Results", )
|
| 249 |
search_btn.click(fn=search, inputs=query, outputs=results, api_name="search", show_progress=True)
|
| 250 |
+
|
| 251 |
|
| 252 |
|
| 253 |
demo.launch()
|
requirements.txt
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
gradio
|
| 2 |
openai
|
| 3 |
requests
|
| 4 |
-
httpx<0.28
|
|
|
|
|
|
| 1 |
gradio
|
| 2 |
openai
|
| 3 |
requests
|
| 4 |
+
httpx<0.28
|
| 5 |
+
google-generativeai
|