Spaces:
Running
Running
gemini
Browse files- app.py +8 -8
- paper_chat_tab.py +73 -23
- requirements.txt +1 -0
app.py
CHANGED
|
@@ -184,14 +184,14 @@ with gr.Blocks(css_paths="style.css") as demo:
|
|
| 184 |
wrap=True,
|
| 185 |
)
|
| 186 |
|
| 187 |
-
with gr.Tab("Edit papers", id="tab-pr"):
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
with gr.Tab("Leaderboards", id="tab-leaderboards"):
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
|
| 196 |
|
| 197 |
# chat with paper
|
|
|
|
| 184 |
wrap=True,
|
| 185 |
)
|
| 186 |
|
| 187 |
+
# with gr.Tab("Edit papers", id="tab-pr"):
|
| 188 |
+
# pr_paper_central_tab(paper_central_df.df_raw)
|
| 189 |
+
#
|
| 190 |
+
# with gr.Tab("Leaderboards", id="tab-leaderboards"):
|
| 191 |
+
# with gr.Tab("Authors"):
|
| 192 |
+
# author_leaderboard_tab()
|
| 193 |
+
# with gr.Tab("Contributors"):
|
| 194 |
+
# author_resource_leaderboard_tab()
|
| 195 |
|
| 196 |
|
| 197 |
# chat with paper
|
paper_chat_tab.py
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from PyPDF2 import PdfReader
|
| 3 |
from bs4 import BeautifulSoup
|
|
@@ -17,6 +19,19 @@ tokenizer_cache = {}
|
|
| 17 |
|
| 18 |
# Global variables for providers
|
| 19 |
PROVIDERS = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
"SambaNova": {
|
| 21 |
"name": "SambaNova",
|
| 22 |
"logo": "https://venturebeat.com/wp-content/uploads/2020/02/SambaNovaLogo_H_F.jpg",
|
|
@@ -24,6 +39,7 @@ PROVIDERS = {
|
|
| 24 |
"api_key_env_var": "SAMBANOVA_API_KEY",
|
| 25 |
"models": [
|
| 26 |
"Meta-Llama-3.1-70B-Instruct",
|
|
|
|
| 27 |
],
|
| 28 |
"type": "tuples",
|
| 29 |
"max_total_tokens": "50000",
|
|
@@ -191,6 +207,7 @@ def create_chat_interface(provider_dropdown, model_dropdown, paper_content, hf_t
|
|
| 191 |
message_tokens_list = []
|
| 192 |
total_tokens = context_token_length
|
| 193 |
|
|
|
|
| 194 |
for user_msg, assistant_msg in history:
|
| 195 |
user_tokens = tokenizer.encode(user_msg)
|
| 196 |
messages.append({"role": "user", "content": user_msg})
|
|
@@ -208,6 +225,7 @@ def create_chat_interface(provider_dropdown, model_dropdown, paper_content, hf_t
|
|
| 208 |
message_tokens_list.append(len(message_tokens))
|
| 209 |
total_tokens += len(message_tokens)
|
| 210 |
|
|
|
|
| 211 |
if total_tokens > max_total_tokens:
|
| 212 |
available_tokens = max_total_tokens - (total_tokens - context_token_length)
|
| 213 |
if available_tokens > 0:
|
|
@@ -227,35 +245,67 @@ def create_chat_interface(provider_dropdown, model_dropdown, paper_content, hf_t
|
|
| 227 |
|
| 228 |
final_messages = []
|
| 229 |
if context:
|
| 230 |
-
final_messages.append(
|
|
|
|
| 231 |
final_messages.extend(messages)
|
| 232 |
|
| 233 |
api_key = hf_token_value or os.environ.get(api_key_env_var)
|
| 234 |
if not api_key:
|
| 235 |
raise ValueError("API token is not provided.")
|
| 236 |
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
)
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 259 |
|
| 260 |
chatbot = gr.Chatbot(label="Chatbot", scale=1, height=800, autoscroll=True)
|
| 261 |
chat_interface = gr.ChatInterface(
|
|
@@ -299,7 +349,7 @@ def paper_chat_tab(paper_id, paper_from, paper_central_df):
|
|
| 299 |
|
| 300 |
# Add a textbox for user to enter a paper_id (arxiv_id)
|
| 301 |
paper_id_input = gr.Textbox(
|
| 302 |
-
label="Or enter a 🤗 paper_id
|
| 303 |
placeholder="e.g. 1234.56789"
|
| 304 |
)
|
| 305 |
|
|
|
|
| 1 |
+
import google.generativeai as genai
|
| 2 |
+
|
| 3 |
import gradio as gr
|
| 4 |
from PyPDF2 import PdfReader
|
| 5 |
from bs4 import BeautifulSoup
|
|
|
|
| 19 |
|
| 20 |
# Global variables for providers
|
| 21 |
PROVIDERS = {
|
| 22 |
+
"Gemini": {
|
| 23 |
+
"name": "Gemini",
|
| 24 |
+
"logo": "https://www.gstatic.com/lamda/images/gemini_thumbnail_c362e5eadc46ca9f617e2.png",
|
| 25 |
+
"endpoint": "https://example-gemini-endpoint", # not need
|
| 26 |
+
# Not necessarily needed for Gemini since we use google.generativeai directly
|
| 27 |
+
"api_key_env_var": "GEMINI_API_KEY", # If using env vars for key storage
|
| 28 |
+
"models": [
|
| 29 |
+
"gemini-2.0-flash-exp",
|
| 30 |
+
"gemini-1.5-flash",
|
| 31 |
+
],
|
| 32 |
+
"type": "tuples",
|
| 33 |
+
"max_total_tokens": "50000",
|
| 34 |
+
},
|
| 35 |
"SambaNova": {
|
| 36 |
"name": "SambaNova",
|
| 37 |
"logo": "https://venturebeat.com/wp-content/uploads/2020/02/SambaNovaLogo_H_F.jpg",
|
|
|
|
| 39 |
"api_key_env_var": "SAMBANOVA_API_KEY",
|
| 40 |
"models": [
|
| 41 |
"Meta-Llama-3.1-70B-Instruct",
|
| 42 |
+
"Meta-Llama-3.3-70B-Instruct",
|
| 43 |
],
|
| 44 |
"type": "tuples",
|
| 45 |
"max_total_tokens": "50000",
|
|
|
|
| 207 |
message_tokens_list = []
|
| 208 |
total_tokens = context_token_length
|
| 209 |
|
| 210 |
+
# Reconstruct the conversation from history and current user message
|
| 211 |
for user_msg, assistant_msg in history:
|
| 212 |
user_tokens = tokenizer.encode(user_msg)
|
| 213 |
messages.append({"role": "user", "content": user_msg})
|
|
|
|
| 225 |
message_tokens_list.append(len(message_tokens))
|
| 226 |
total_tokens += len(message_tokens)
|
| 227 |
|
| 228 |
+
# Token truncation logic
|
| 229 |
if total_tokens > max_total_tokens:
|
| 230 |
available_tokens = max_total_tokens - (total_tokens - context_token_length)
|
| 231 |
if available_tokens > 0:
|
|
|
|
| 245 |
|
| 246 |
final_messages = []
|
| 247 |
if context:
|
| 248 |
+
final_messages.append(
|
| 249 |
+
{"role": "system" if not provider_name_value == "Gemini" else "user", "content": f"{context}"})
|
| 250 |
final_messages.extend(messages)
|
| 251 |
|
| 252 |
api_key = hf_token_value or os.environ.get(api_key_env_var)
|
| 253 |
if not api_key:
|
| 254 |
raise ValueError("API token is not provided.")
|
| 255 |
|
| 256 |
+
# Gemini logic
|
| 257 |
+
if provider_name_value == "Gemini":
|
| 258 |
+
import google.generativeai as genai
|
| 259 |
+
genai.configure(api_key=api_key)
|
| 260 |
+
|
| 261 |
+
# According to the docs, model should be instantiated with full model name, e.g. "models/gemini-1.5-flash"
|
| 262 |
+
# Ensure your PROVIDERS dict sets the model_name_value accordingly (e.g. "models/gemini-1.5-flash")
|
| 263 |
+
model = genai.GenerativeModel(model_name=model_name_value)
|
| 264 |
+
|
| 265 |
+
# Convert final_messages into Gemini's format:
|
| 266 |
+
# Gemini expects a list of messages: [{"role": "user"/"assistant"/"system", "parts": ["..."]}, ...]
|
| 267 |
+
gemini_messages = []
|
| 268 |
+
for m in final_messages:
|
| 269 |
+
gemini_messages.append({"role": m["role"], "parts": [m["content"]]})
|
| 270 |
+
|
| 271 |
+
# Now call generate_content with stream=True
|
| 272 |
+
try:
|
| 273 |
+
response = model.generate_content(gemini_messages, stream=True)
|
| 274 |
+
response_text = ""
|
| 275 |
+
for chunk in response:
|
| 276 |
+
if chunk.text:
|
| 277 |
+
response_text += chunk.text
|
| 278 |
+
yield response_text
|
| 279 |
+
except Exception as ex:
|
| 280 |
+
yield f"Error calling Gemini: {ex}"
|
| 281 |
+
else:
|
| 282 |
+
# Default OpenAI-compatible logic
|
| 283 |
+
from openai import OpenAI
|
| 284 |
+
import openai
|
| 285 |
+
import json
|
| 286 |
+
|
| 287 |
+
client = OpenAI(
|
| 288 |
+
base_url=endpoint,
|
| 289 |
+
api_key=api_key,
|
| 290 |
)
|
| 291 |
+
|
| 292 |
+
try:
|
| 293 |
+
completion = client.chat.completions.create(
|
| 294 |
+
model=model_name_value,
|
| 295 |
+
messages=final_messages,
|
| 296 |
+
stream=True,
|
| 297 |
+
)
|
| 298 |
+
response_text = ""
|
| 299 |
+
for chunk in completion:
|
| 300 |
+
delta = chunk.choices[0].delta.content or ""
|
| 301 |
+
response_text += delta
|
| 302 |
+
yield response_text
|
| 303 |
+
except json.JSONDecodeError as e:
|
| 304 |
+
yield f"JSON decoding error: {e.msg}"
|
| 305 |
+
except openai.OpenAIError as openai_err:
|
| 306 |
+
yield f"OpenAI error: {openai_err}"
|
| 307 |
+
except Exception as ex:
|
| 308 |
+
yield f"Unexpected error: {ex}"
|
| 309 |
|
| 310 |
chatbot = gr.Chatbot(label="Chatbot", scale=1, height=800, autoscroll=True)
|
| 311 |
chat_interface = gr.ChatInterface(
|
|
|
|
| 349 |
|
| 350 |
# Add a textbox for user to enter a paper_id (arxiv_id)
|
| 351 |
paper_id_input = gr.Textbox(
|
| 352 |
+
label="Or enter a 🤗 paper_id directly",
|
| 353 |
placeholder="e.g. 1234.56789"
|
| 354 |
)
|
| 355 |
|
requirements.txt
CHANGED
|
@@ -7,6 +7,7 @@ PyPDF2
|
|
| 7 |
transformers
|
| 8 |
beautifulsoup4
|
| 9 |
openai
|
|
|
|
| 10 |
# Set the primary index URL to PyTorch's CPU wheels
|
| 11 |
--index-url https://download.pytorch.org/whl/cpu
|
| 12 |
|
|
|
|
| 7 |
transformers
|
| 8 |
beautifulsoup4
|
| 9 |
openai
|
| 10 |
+
google-generativeai
|
| 11 |
# Set the primary index URL to PyTorch's CPU wheels
|
| 12 |
--index-url https://download.pytorch.org/whl/cpu
|
| 13 |
|