Spaces:
Runtime error
Runtime error
eljanmahammadli
commited on
Commit
·
f716a54
1
Parent(s):
3dae562
changed LLM client choices (gemini, claude, GPT-4o-mini)
Browse files- ai_generate.py +51 -27
- app.py +35 -24
- requirements.txt +3 -1
ai_generate.py
CHANGED
@@ -3,6 +3,8 @@ from openai import OpenAI
|
|
3 |
import os
|
4 |
from transformers import pipeline
|
5 |
from groq import Groq
|
|
|
|
|
6 |
from langchain_community.document_loaders import PyMuPDFLoader
|
7 |
from langchain_community.document_loaders import TextLoader
|
8 |
from langchain_community.embeddings.sentence_transformer import (
|
@@ -19,9 +21,20 @@ from dotenv import load_dotenv
|
|
19 |
|
20 |
load_dotenv()
|
21 |
|
|
|
|
|
|
|
22 |
groq_client = Groq(
|
23 |
api_key=os.environ.get("GROQ_API_KEY"),
|
24 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
|
27 |
def create_db_with_langchain(path):
|
@@ -67,7 +80,6 @@ def generate_groq_base(text, model):
|
|
67 |
],
|
68 |
temperature=1,
|
69 |
max_tokens=1024,
|
70 |
-
top_p=1,
|
71 |
stream=True,
|
72 |
stop=None,
|
73 |
)
|
@@ -88,35 +100,47 @@ def generate_groq(text, model, path):
|
|
88 |
def generate_openai(text, model, openai_client):
|
89 |
message = [{"role": "user", "content": text}]
|
90 |
response = openai_client.chat.completions.create(
|
91 |
-
model=model,
|
|
|
|
|
|
|
92 |
)
|
93 |
return response.choices[0].message.content
|
94 |
|
95 |
|
96 |
-
def
|
97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
return generate_groq(text, "llama3-70b-8192", path)
|
99 |
-
elif model == "
|
100 |
-
return
|
101 |
-
elif model == "Mistral":
|
102 |
-
return generate_groq(text, "mixtral-8x7b-32768", path)
|
103 |
-
elif model == "Gemma":
|
104 |
-
return generate_groq(text, "gemma2-9b-it", path)
|
105 |
-
elif model == "OpenAI GPT 3.5":
|
106 |
-
try:
|
107 |
-
openai_client = OpenAI(api_key=api)
|
108 |
-
return generate_openai(text, "gpt-3.5-turbo", openai_client)
|
109 |
-
except:
|
110 |
-
return "Please add a valid API key"
|
111 |
-
elif model == "OpenAI GPT 4":
|
112 |
-
try:
|
113 |
-
openai_client = OpenAI(api_key=api)
|
114 |
-
return generate_openai(text, "gpt-4-turbo", openai_client)
|
115 |
-
except:
|
116 |
-
return "Please add a valid API key"
|
117 |
elif model == "OpenAI GPT 4o":
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
|
|
|
|
|
3 |
import os
|
4 |
from transformers import pipeline
|
5 |
from groq import Groq
|
6 |
+
import google.generativeai as genai
|
7 |
+
import anthropic
|
8 |
from langchain_community.document_loaders import PyMuPDFLoader
|
9 |
from langchain_community.document_loaders import TextLoader
|
10 |
from langchain_community.embeddings.sentence_transformer import (
|
|
|
21 |
|
22 |
load_dotenv()
|
23 |
|
24 |
+
os.environ["GRPC_VERBOSITY"] = "ERROR"
|
25 |
+
os.environ["GLOG_minloglevel"] = "2"
|
26 |
+
|
27 |
groq_client = Groq(
|
28 |
api_key=os.environ.get("GROQ_API_KEY"),
|
29 |
)
|
30 |
+
openai_client = OpenAI(api_key=os.environ.get("OPENAI_API_KEY"))
|
31 |
+
# give access to all APIs for GCP instance
|
32 |
+
# gcloud auth application-default login
|
33 |
+
genai.configure(api_key=os.environ.get("GENAI_API_KEY"))
|
34 |
+
gemini_client = genai.GenerativeModel(
|
35 |
+
model_name=f"models/gemini-1.5-pro", system_instruction="You are helpful assistant."
|
36 |
+
)
|
37 |
+
claude_client = anthropic.Anthropic(api_key=os.environ.get("ANTHROPIC_API_KEY"))
|
38 |
|
39 |
|
40 |
def create_db_with_langchain(path):
|
|
|
80 |
],
|
81 |
temperature=1,
|
82 |
max_tokens=1024,
|
|
|
83 |
stream=True,
|
84 |
stop=None,
|
85 |
)
|
|
|
100 |
def generate_openai(text, model, openai_client):
|
101 |
message = [{"role": "user", "content": text}]
|
102 |
response = openai_client.chat.completions.create(
|
103 |
+
model=model,
|
104 |
+
messages=message,
|
105 |
+
temperature=1,
|
106 |
+
max_tokens=1024,
|
107 |
)
|
108 |
return response.choices[0].message.content
|
109 |
|
110 |
|
111 |
+
def generate_gemini(text, model, gemini_client):
|
112 |
+
response = gemini_client.generate_content(
|
113 |
+
text,
|
114 |
+
generation_config={
|
115 |
+
"max_output_tokens": 1024,
|
116 |
+
"temperature": 1,
|
117 |
+
},
|
118 |
+
)
|
119 |
+
return response.text.strip()
|
120 |
+
|
121 |
+
|
122 |
+
def generate_claude(text, model, claude_client):
|
123 |
+
response = claude_client.messages.create(
|
124 |
+
model=model,
|
125 |
+
max_tokens=1024,
|
126 |
+
temperature=1.0,
|
127 |
+
system="You are helpful assistant.",
|
128 |
+
messages=[{"role": "user", "content": [{"type": "text", "text": text}]}],
|
129 |
+
)
|
130 |
+
return response.content[0].text.strip()
|
131 |
+
|
132 |
+
|
133 |
+
def generate(text, model, path, api=None):
|
134 |
+
|
135 |
+
if model == "LLaMA 3":
|
136 |
return generate_groq(text, "llama3-70b-8192", path)
|
137 |
+
elif model == "OpenAI GPT 4o Mini":
|
138 |
+
return generate_openai(text, "gpt-4o-mini", openai_client)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
139 |
elif model == "OpenAI GPT 4o":
|
140 |
+
return generate_openai(text, "gpt-4o", openai_client)
|
141 |
+
elif model == "OpenAI GPT 4":
|
142 |
+
return generate_openai(text, "gpt-4-turbo", openai_client)
|
143 |
+
elif model == "Gemini 1.5 Pro":
|
144 |
+
return generate_gemini(text, "", gemini_client)
|
145 |
+
elif model == "Claude Sonnet 3.5":
|
146 |
+
return generate_claude(text, "claude-3-5-sonnet-20240620", claude_client)
|
app.py
CHANGED
@@ -2,8 +2,6 @@ import openai
|
|
2 |
import gradio as gr
|
3 |
from typing import Dict, List
|
4 |
import re
|
5 |
-
from humanize import paraphrase_text
|
6 |
-
from ai_generate import generate
|
7 |
import requests
|
8 |
import language_tool_python
|
9 |
import torch
|
@@ -13,12 +11,13 @@ from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipe
|
|
13 |
from scipy.special import softmax
|
14 |
from collections import defaultdict
|
15 |
import nltk
|
|
|
|
|
16 |
from utils import remove_special_characters
|
17 |
from plagiarism import google_search, months, domain_list, build_date
|
18 |
-
from
|
|
|
19 |
|
20 |
-
# Check if CUDA is available
|
21 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
22 |
print(f"Using device: {device}")
|
23 |
|
24 |
models = {
|
@@ -65,7 +64,7 @@ def format_and_correct(text: str) -> str:
|
|
65 |
Please correct the formatting, grammar, and spelling errors in the following text without changing its content significantly. Ensure proper paragraph breaks and maintain the original content:
|
66 |
{text}
|
67 |
"""
|
68 |
-
corrected_text = generate(prompt, "
|
69 |
return clean_text(corrected_text)
|
70 |
|
71 |
|
@@ -287,7 +286,7 @@ def generate_article(
|
|
287 |
conclusion_type: str,
|
288 |
ai_model: str,
|
289 |
content_string: str,
|
290 |
-
api_key: str = None,
|
291 |
pdf_file_input=None,
|
292 |
generated_article: str = None,
|
293 |
user_comments: str = None,
|
@@ -317,7 +316,8 @@ def generate_article(
|
|
317 |
prompt = generate_prompt(settings)
|
318 |
|
319 |
print(prompt)
|
320 |
-
|
|
|
321 |
response = openai.ChatCompletion.create(
|
322 |
model="gpt-4" if ai_model == "OpenAI GPT 4" else "gpt-3.5-turbo",
|
323 |
messages=[
|
@@ -334,7 +334,11 @@ def generate_article(
|
|
334 |
)
|
335 |
article = response.choices[0].message.content.strip()
|
336 |
else:
|
337 |
-
article = generate(
|
|
|
|
|
|
|
|
|
338 |
|
339 |
return clean_text(article)
|
340 |
|
@@ -407,7 +411,7 @@ def generate_and_format(
|
|
407 |
num_examples,
|
408 |
conclusion_type,
|
409 |
ai_model,
|
410 |
-
api_key,
|
411 |
google_search_check,
|
412 |
year_from,
|
413 |
month_from,
|
@@ -457,7 +461,7 @@ def generate_and_format(
|
|
457 |
conclusion_type,
|
458 |
ai_model,
|
459 |
content_string,
|
460 |
-
api_key,
|
461 |
pdf_file_input,
|
462 |
generated_article,
|
463 |
user_comments,
|
@@ -624,6 +628,13 @@ def create_interface():
|
|
624 |
placeholder="Enter comma-separated keywords",
|
625 |
elem_classes="input-highlight-yellow",
|
626 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
627 |
with gr.Row():
|
628 |
month_from = gr.Dropdown(
|
629 |
choices=months,
|
@@ -644,26 +655,26 @@ def create_interface():
|
|
644 |
day_to = gr.Textbox(label="To Day", value=d1[0])
|
645 |
year_to = gr.Textbox(label="To Year", value=d1[2])
|
646 |
|
647 |
-
with gr.Row():
|
648 |
-
domains_to_include = gr.Dropdown(
|
649 |
-
domain_list,
|
650 |
-
value=domain_list,
|
651 |
-
multiselect=True,
|
652 |
-
label="Domains To Include",
|
653 |
-
)
|
654 |
gr.Markdown("# Add Optional PDF File with Information", elem_classes="text-center text-3xl mb-6")
|
655 |
pdf_file_input = gr.File(label="Upload PDF")
|
656 |
|
657 |
with gr.Group():
|
658 |
gr.Markdown("## AI Model Configuration", elem_classes="text-xl mb-4")
|
659 |
ai_generator = gr.Dropdown(
|
660 |
-
choices=[
|
661 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
662 |
label="AI Model",
|
663 |
elem_classes="input-highlight-pink",
|
664 |
)
|
665 |
-
input_api = gr.Textbox(label="API Key", visible=False)
|
666 |
-
ai_generator.change(update_visibility_api, ai_generator, input_api)
|
667 |
|
668 |
generate_btn = gr.Button("Generate Article", variant="primary")
|
669 |
|
@@ -754,7 +765,7 @@ def create_interface():
|
|
754 |
input_num_examples,
|
755 |
input_conclusion,
|
756 |
ai_generator,
|
757 |
-
input_api,
|
758 |
google_search_check,
|
759 |
year_from,
|
760 |
month_from,
|
@@ -787,7 +798,7 @@ def create_interface():
|
|
787 |
input_num_examples,
|
788 |
input_conclusion,
|
789 |
ai_generator,
|
790 |
-
input_api,
|
791 |
google_search_check,
|
792 |
year_from,
|
793 |
month_from,
|
|
|
2 |
import gradio as gr
|
3 |
from typing import Dict, List
|
4 |
import re
|
|
|
|
|
5 |
import requests
|
6 |
import language_tool_python
|
7 |
import torch
|
|
|
11 |
from scipy.special import softmax
|
12 |
from collections import defaultdict
|
13 |
import nltk
|
14 |
+
from datetime import date
|
15 |
+
|
16 |
from utils import remove_special_characters
|
17 |
from plagiarism import google_search, months, domain_list, build_date
|
18 |
+
from humanize import paraphrase_text, device
|
19 |
+
from ai_generate import generate
|
20 |
|
|
|
|
|
21 |
print(f"Using device: {device}")
|
22 |
|
23 |
models = {
|
|
|
64 |
Please correct the formatting, grammar, and spelling errors in the following text without changing its content significantly. Ensure proper paragraph breaks and maintain the original content:
|
65 |
{text}
|
66 |
"""
|
67 |
+
corrected_text = generate(prompt, "Llama 3", None)
|
68 |
return clean_text(corrected_text)
|
69 |
|
70 |
|
|
|
286 |
conclusion_type: str,
|
287 |
ai_model: str,
|
288 |
content_string: str,
|
289 |
+
# api_key: str = None,
|
290 |
pdf_file_input=None,
|
291 |
generated_article: str = None,
|
292 |
user_comments: str = None,
|
|
|
316 |
prompt = generate_prompt(settings)
|
317 |
|
318 |
print(prompt)
|
319 |
+
# TODO: Why do we need this ??
|
320 |
+
if ai_model in ["OpenAI GPT 3.5"]:
|
321 |
response = openai.ChatCompletion.create(
|
322 |
model="gpt-4" if ai_model == "OpenAI GPT 4" else "gpt-3.5-turbo",
|
323 |
messages=[
|
|
|
334 |
)
|
335 |
article = response.choices[0].message.content.strip()
|
336 |
else:
|
337 |
+
article = generate(
|
338 |
+
prompt,
|
339 |
+
ai_model,
|
340 |
+
pdf_file_input, # api_key
|
341 |
+
)
|
342 |
|
343 |
return clean_text(article)
|
344 |
|
|
|
411 |
num_examples,
|
412 |
conclusion_type,
|
413 |
ai_model,
|
414 |
+
# api_key,
|
415 |
google_search_check,
|
416 |
year_from,
|
417 |
month_from,
|
|
|
461 |
conclusion_type,
|
462 |
ai_model,
|
463 |
content_string,
|
464 |
+
# api_key,
|
465 |
pdf_file_input,
|
466 |
generated_article,
|
467 |
user_comments,
|
|
|
628 |
placeholder="Enter comma-separated keywords",
|
629 |
elem_classes="input-highlight-yellow",
|
630 |
)
|
631 |
+
with gr.Row():
|
632 |
+
domains_to_include = gr.Dropdown(
|
633 |
+
domain_list,
|
634 |
+
value=domain_list,
|
635 |
+
multiselect=True,
|
636 |
+
label="Domains To Include",
|
637 |
+
)
|
638 |
with gr.Row():
|
639 |
month_from = gr.Dropdown(
|
640 |
choices=months,
|
|
|
655 |
day_to = gr.Textbox(label="To Day", value=d1[0])
|
656 |
year_to = gr.Textbox(label="To Year", value=d1[2])
|
657 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
658 |
gr.Markdown("# Add Optional PDF File with Information", elem_classes="text-center text-3xl mb-6")
|
659 |
pdf_file_input = gr.File(label="Upload PDF")
|
660 |
|
661 |
with gr.Group():
|
662 |
gr.Markdown("## AI Model Configuration", elem_classes="text-xl mb-4")
|
663 |
ai_generator = gr.Dropdown(
|
664 |
+
choices=[
|
665 |
+
"OpenAI GPT 4",
|
666 |
+
"OpenAI GPT 4o",
|
667 |
+
"OpenAI GPT 4o Mini",
|
668 |
+
"Claude Sonnet 3.5",
|
669 |
+
"Gemini 1.5 Pro",
|
670 |
+
"LLaMA 3",
|
671 |
+
],
|
672 |
+
value="OpenAI GPT 4o Mini",
|
673 |
label="AI Model",
|
674 |
elem_classes="input-highlight-pink",
|
675 |
)
|
676 |
+
# input_api = gr.Textbox(label="API Key", visible=False)
|
677 |
+
# ai_generator.change(update_visibility_api, ai_generator, input_api)
|
678 |
|
679 |
generate_btn = gr.Button("Generate Article", variant="primary")
|
680 |
|
|
|
765 |
input_num_examples,
|
766 |
input_conclusion,
|
767 |
ai_generator,
|
768 |
+
# input_api,
|
769 |
google_search_check,
|
770 |
year_from,
|
771 |
month_from,
|
|
|
798 |
input_num_examples,
|
799 |
input_conclusion,
|
800 |
ai_generator,
|
801 |
+
# input_api,
|
802 |
google_search_check,
|
803 |
year_from,
|
804 |
month_from,
|
requirements.txt
CHANGED
@@ -19,4 +19,6 @@ sentence-transformers
|
|
19 |
langchain-community
|
20 |
pymupdf
|
21 |
chromadb
|
22 |
-
language-tool-python
|
|
|
|
|
|
19 |
langchain-community
|
20 |
pymupdf
|
21 |
chromadb
|
22 |
+
language-tool-python
|
23 |
+
anthropic
|
24 |
+
google-generativeai
|