Spaces:
Sleeping
Sleeping
updated to work with Gemini
Browse files- app.py +112 -89
- 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
|
|
@@ -7,6 +9,8 @@ import time
|
|
| 7 |
import re
|
| 8 |
#export GRADIO_DEBUG=1
|
| 9 |
|
|
|
|
|
|
|
| 10 |
def search_inspire(query, size=10):
|
| 11 |
"""
|
| 12 |
Search INSPIRE HEP database using fulltext search
|
|
@@ -70,97 +74,112 @@ def user_prompt(query, context):
|
|
| 70 |
def llm_expand_query(query):
|
| 71 |
""" Expands a query to variations of fulltext searches """
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
|
| 119 |
def llm_generate_answer(prompt):
|
| 120 |
""" Generate a response from the LLM """
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 142 |
},
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
},
|
| 156 |
-
temperature=0,
|
| 157 |
-
max_tokens=2048,
|
| 158 |
-
top_p=1,
|
| 159 |
-
frequency_penalty=0,
|
| 160 |
-
presence_penalty=0
|
| 161 |
-
)
|
| 162 |
-
|
| 163 |
-
return response.choices[0].message.content
|
| 164 |
|
| 165 |
def clean_refs(answer, results):
|
| 166 |
""" Clean the references from the answer """
|
|
@@ -209,7 +228,10 @@ def search(query, progress=gr.Progress()):
|
|
| 209 |
|
| 210 |
# ----------- MAIN ------------------------------------------------------------
|
| 211 |
|
| 212 |
-
|
|
|
|
|
|
|
|
|
|
| 213 |
|
| 214 |
with gr.Blocks() as demo:
|
| 215 |
gr.Markdown("# Feynbot on INSPIRE HEP Search")
|
|
@@ -221,10 +243,11 @@ with gr.Blocks() as demo:
|
|
| 221 |
query = gr.Textbox(label="Search Query")
|
| 222 |
search_btn = gr.Button("Search")
|
| 223 |
examples = gr.Examples([["Which one is closest star?"], ["In which particles does the Higgs Boson decay to?"]], query)
|
|
|
|
| 224 |
with gr.Column():
|
| 225 |
results = gr.Markdown("Answer will appear here...", label="Search Results", )
|
| 226 |
search_btn.click(fn=search, inputs=query, outputs=results, api_name="search", show_progress=True)
|
| 227 |
-
|
| 228 |
|
| 229 |
|
| 230 |
demo.launch()
|
|
|
|
| 1 |
from openai import OpenAI
|
| 2 |
+
import google.generativeai as genai
|
| 3 |
+
|
| 4 |
import os
|
| 5 |
import requests
|
| 6 |
import json
|
|
|
|
| 9 |
import re
|
| 10 |
#export GRADIO_DEBUG=1
|
| 11 |
|
| 12 |
+
GENAI_API = "gemini" # or "openai"
|
| 13 |
+
|
| 14 |
def search_inspire(query, size=10):
|
| 15 |
"""
|
| 16 |
Search INSPIRE HEP database using fulltext search
|
|
|
|
| 74 |
def llm_expand_query(query):
|
| 75 |
""" Expands a query to variations of fulltext searches """
|
| 76 |
|
| 77 |
+
|
| 78 |
+
prompt = f"""
|
| 79 |
+
Expand this query into a the query format used for a fulltext search
|
| 80 |
+
over the INSPIRE HEP database. Propose alternatives of the query to
|
| 81 |
+
maximize the recall and join those variantes using OR operators and
|
| 82 |
+
prepend each variant with the ft prefix. Just provide the expanded
|
| 83 |
+
query, without explanations.
|
| 84 |
+
|
| 85 |
+
Example of query:
|
| 86 |
+
how far are black holes?
|
| 87 |
+
|
| 88 |
+
Expanded query:
|
| 89 |
+
ft "how far are black holes" OR ft "distance from black holes" OR ft
|
| 90 |
+
"distances to black holes" OR ft "measurement of distance to black
|
| 91 |
+
holes" OR ft "remoteness of black holes" OR ft "distance to black
|
| 92 |
+
holes" OR ft "how far are singularities" OR ft "distance to
|
| 93 |
+
singularities" OR ft "distances to event horizon" OR ft "distance
|
| 94 |
+
from Schwarzschild radius" OR ft "black hole distance"
|
| 95 |
+
|
| 96 |
+
Query: {query}
|
| 97 |
+
|
| 98 |
+
Expanded query:
|
| 99 |
+
"""
|
| 100 |
+
|
| 101 |
+
if GENAI_API == "openai":
|
| 102 |
+
response = client.chat.completions.create(
|
| 103 |
+
model="gpt-4o-mini",
|
| 104 |
+
messages=[
|
| 105 |
+
{
|
| 106 |
+
"role": "user",
|
| 107 |
+
"content": [
|
| 108 |
+
{
|
| 109 |
+
"type": "text",
|
| 110 |
+
"text": prompt
|
| 111 |
+
}
|
| 112 |
+
]
|
| 113 |
+
}
|
| 114 |
+
],
|
| 115 |
+
response_format={
|
| 116 |
+
"type": "text"
|
| 117 |
+
},
|
| 118 |
+
temperature=0,
|
| 119 |
+
max_tokens=2048,
|
| 120 |
+
top_p=1,
|
| 121 |
+
frequency_penalty=0,
|
| 122 |
+
presence_penalty=0
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
return response.choices[0].message.content
|
| 126 |
+
else:
|
| 127 |
+
response = genai.GenerativeModel("gemini-1.5-flash").generate_content(prompt)
|
| 128 |
+
return response.text
|
| 129 |
|
| 130 |
def llm_generate_answer(prompt):
|
| 131 |
""" Generate a response from the LLM """
|
| 132 |
+
|
| 133 |
+
system_desc = """You are part of a Retrieval Augmented Generation system
|
| 134 |
+
(RAG) and are asked with a query and a context of results. Generate an
|
| 135 |
+
answer substantiated by the results provided and citing them using
|
| 136 |
+
their index when used to provide an answer text. Do not put two or more
|
| 137 |
+
references together (ex: use [1][2] instead of [1,2]. Do not generate an answer
|
| 138 |
+
that cannot be entailed from cited abstract, so all paragraphs should cite a
|
| 139 |
+
search result. End the answer with the query and a brief answer as
|
| 140 |
+
summary of the previous discussed results. Do not consider results
|
| 141 |
+
that are not related to the query and, if no specific answer can be
|
| 142 |
+
provided, assert that in the brief answer."""
|
| 143 |
+
|
| 144 |
+
if GENAI_API == "openai":
|
| 145 |
+
|
| 146 |
+
response = client.chat.completions.create(
|
| 147 |
+
model="gpt-4o-mini",
|
| 148 |
+
messages=[
|
| 149 |
+
{
|
| 150 |
+
"role": "system",
|
| 151 |
+
"content": [
|
| 152 |
+
{
|
| 153 |
+
"type": "text",
|
| 154 |
+
"text": system_desc
|
| 155 |
+
}
|
| 156 |
+
]
|
| 157 |
+
},
|
| 158 |
+
{
|
| 159 |
+
"role": "user",
|
| 160 |
+
"content": [
|
| 161 |
+
{
|
| 162 |
+
"type": "text",
|
| 163 |
+
"text": prompt
|
| 164 |
+
}
|
| 165 |
+
]
|
| 166 |
+
}
|
| 167 |
+
],
|
| 168 |
+
response_format={
|
| 169 |
+
"type": "text"
|
| 170 |
},
|
| 171 |
+
temperature=0,
|
| 172 |
+
max_tokens=2048,
|
| 173 |
+
top_p=1,
|
| 174 |
+
frequency_penalty=0,
|
| 175 |
+
presence_penalty=0
|
| 176 |
+
)
|
| 177 |
+
|
| 178 |
+
return response.choices[0].message.content
|
| 179 |
+
|
| 180 |
+
else:
|
| 181 |
+
response = genai.GenerativeModel("gemini-1.5-flash").generate_content(system_desc + "\n\n" + prompt)
|
| 182 |
+
return response.text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
|
| 184 |
def clean_refs(answer, results):
|
| 185 |
""" Clean the references from the answer """
|
|
|
|
| 228 |
|
| 229 |
# ----------- MAIN ------------------------------------------------------------
|
| 230 |
|
| 231 |
+
if GENAI_API == "openai":
|
| 232 |
+
client = OpenAI()
|
| 233 |
+
else:
|
| 234 |
+
genai.configure(api_key=os.getenv('GEMINI_API_KEY'))
|
| 235 |
|
| 236 |
with gr.Blocks() as demo:
|
| 237 |
gr.Markdown("# Feynbot on INSPIRE HEP Search")
|
|
|
|
| 243 |
query = gr.Textbox(label="Search Query")
|
| 244 |
search_btn = gr.Button("Search")
|
| 245 |
examples = gr.Examples([["Which one is closest star?"], ["In which particles does the Higgs Boson decay to?"]], query)
|
| 246 |
+
gr.HTML("<img src='https://sinai.ujaen.es/sites/default/files/SINAI%20-%20logo%20tx%20azul%20%5Baf%5D.png' width='200'></img>")
|
| 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
|