Roseco-crs
commited on
Commit
·
acfcb6f
1
Parent(s):
98a3cb2
NaanhAI project using Gradio as interface.
Browse files- README.md +1 -1
- ailab_crs.py +123 -0
- app.py +62 -0
- pyproject.toml +14 -0
- requirements.txt +6 -0
README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
---
|
| 2 |
title: NaanhAI
|
| 3 |
-
emoji:
|
| 4 |
colorFrom: indigo
|
| 5 |
colorTo: green
|
| 6 |
sdk: gradio
|
|
|
|
| 1 |
---
|
| 2 |
title: NaanhAI
|
| 3 |
+
emoji: 🧠💡
|
| 4 |
colorFrom: indigo
|
| 5 |
colorTo: green
|
| 6 |
sdk: gradio
|
ailab_crs.py
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_groq import ChatGroq
|
| 2 |
+
from langchain_core.prompts import ChatPromptTemplate
|
| 3 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 4 |
+
from dotenv import load_dotenv, find_dotenv
|
| 5 |
+
import os, sys, getpass
|
| 6 |
+
from groq import Groq
|
| 7 |
+
import gradio as gr
|
| 8 |
+
|
| 9 |
+
_ = load_dotenv(find_dotenv())
|
| 10 |
+
groq_api_key = os.environ["GROQ_API_KEY"]
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
class NLP_tasks_crs:
|
| 14 |
+
|
| 15 |
+
@classmethod
|
| 16 |
+
def translator(cls, text: str="Hello world!", language: str="French", style: str="polite"):
|
| 17 |
+
# call our prompt engineering class
|
| 18 |
+
prompt = Prompt_engineering_crs(text, language, style)
|
| 19 |
+
prompt = prompt.translator_prompt()
|
| 20 |
+
# call our LLM_crs class
|
| 21 |
+
llm = LLM_crs()
|
| 22 |
+
llm = llm.chain_llm()
|
| 23 |
+
result = llm.invoke(prompt)
|
| 24 |
+
return result
|
| 25 |
+
|
| 26 |
+
@classmethod
|
| 27 |
+
def summarization(cls, text: str):
|
| 28 |
+
# call our prompt engineering class
|
| 29 |
+
prompt = Prompt_engineering_crs(text)
|
| 30 |
+
prompt = prompt.summarization_prompt()
|
| 31 |
+
# call our LLM_crs class
|
| 32 |
+
llm = LLM_crs()
|
| 33 |
+
llm = llm.chain_llm()
|
| 34 |
+
result = llm.invoke(prompt)
|
| 35 |
+
return result
|
| 36 |
+
|
| 37 |
+
@classmethod
|
| 38 |
+
def translator_summarization(cls, text: str, language: str, style: str="polite"):
|
| 39 |
+
# call our prompt engineering class
|
| 40 |
+
prompt = Prompt_engineering_crs(text, language, style)
|
| 41 |
+
prompt = prompt.translate_summarize_prompt()
|
| 42 |
+
# call our LLM_crs class
|
| 43 |
+
llm = LLM_crs()
|
| 44 |
+
llm = llm.chain_llm()
|
| 45 |
+
result = llm.invoke(prompt)
|
| 46 |
+
return result
|
| 47 |
+
|
| 48 |
+
@classmethod
|
| 49 |
+
def question_answer(cls, question: str):
|
| 50 |
+
# call our prompt engineering class
|
| 51 |
+
prompt = Prompt_engineering_crs()
|
| 52 |
+
prompt = prompt.question_answer_prompt(question)
|
| 53 |
+
# call our LLM_crs class
|
| 54 |
+
llm = LLM_crs()
|
| 55 |
+
llm = llm.chain_llm()
|
| 56 |
+
result = llm.invoke(prompt)
|
| 57 |
+
return result
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
class Prompt_engineering_crs:
|
| 61 |
+
def __init__(self, text: str="Hello world.", language: str="English", style: str="calm and respectful"):
|
| 62 |
+
self.text= text
|
| 63 |
+
self.language= language
|
| 64 |
+
self.style=style
|
| 65 |
+
|
| 66 |
+
def translator_prompt(self):
|
| 67 |
+
template = """You are the best expert translator of human languagues. Your role is to
|
| 68 |
+
firstly detect the correct language of the user text, which is delimited by three
|
| 69 |
+
backticks. Secondly, translate that text into the desired language provided by the user, which is
|
| 70 |
+
{language}. May sure to use {style} tone as your style. Finally, make sure to sound like a formal native speaker and provide only the final result without
|
| 71 |
+
additional information. Thanks.
|
| 72 |
+
text: ```{text}``` """
|
| 73 |
+
prompt_template = ChatPromptTemplate.from_template(template)
|
| 74 |
+
prompt = prompt_template.format_messages(text=self.text, language=self.language, style=self.style)
|
| 75 |
+
return prompt
|
| 76 |
+
|
| 77 |
+
def summarization_prompt(self):
|
| 78 |
+
summary = """ You are the best expert summarizer in the world. Your role is to summarize the user text into the detected language.
|
| 79 |
+
The provided text is below and between three backticks. Make sure to keep the right context. Don't forget to give
|
| 80 |
+
a title to the result of the summarization. Finally, make sure to sound like a formal native speaker and provide
|
| 81 |
+
only the final result without additional information or comments. Thanks.
|
| 82 |
+
text: ```{text}```"""
|
| 83 |
+
prompt_template = ChatPromptTemplate.from_template(summary)
|
| 84 |
+
prompt = prompt_template.format_messages(text=self.text)
|
| 85 |
+
return prompt
|
| 86 |
+
|
| 87 |
+
def translate_summarize_prompt(self, language: str="English"):
|
| 88 |
+
template =""" You are the best translator and summarizer in the world. Your first role is to translate
|
| 89 |
+
the below text into {language} language. Indeed, use the below style during the
|
| 90 |
+
translation. Your second role is to summarize into {language} language, the result of the translation
|
| 91 |
+
with clear and concise words and expressions. Furthermore, use little imojis during
|
| 92 |
+
the translation. Finally, make sure to sound like a formal native speaker and provide only the final result without
|
| 93 |
+
additional information or comments. Thanks.
|
| 94 |
+
text: {text}
|
| 95 |
+
language: {language}
|
| 96 |
+
style: {style} """
|
| 97 |
+
prompt_template = ChatPromptTemplate.from_template(template)
|
| 98 |
+
prompt = prompt_template.format_messages(text=self.text, language=self.language, style=self.style)
|
| 99 |
+
return prompt
|
| 100 |
+
|
| 101 |
+
@classmethod
|
| 102 |
+
def question_answer_prompt(cls, question: str):
|
| 103 |
+
template = "You are a master of questions and answers. Here, your role is to answer to any questions from " \
|
| 104 |
+
"from the user. If you do not know any questions, please state that you don't know them. Don't " \
|
| 105 |
+
"invent answers for the questions you do not know. If you have many answers for a question, provide the accurate" \
|
| 106 |
+
"ones to the user. Use a polite style to communicate with the user. Indeed, speak like a native speaker. See below the question of the user. " \
|
| 107 |
+
"question: {question}"
|
| 108 |
+
prompt_template = ChatPromptTemplate.from_template(template)
|
| 109 |
+
prompt = prompt_template.format_messages(question = {question})
|
| 110 |
+
return prompt
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
class LLM_crs:
|
| 114 |
+
def __init__(self, model="moonshotai/kimi-k2-instruct-0905"):
|
| 115 |
+
self.model = model
|
| 116 |
+
|
| 117 |
+
def chain_llm(self):
|
| 118 |
+
llm = ChatGroq(model = self.model)
|
| 119 |
+
parser = StrOutputParser()
|
| 120 |
+
chain_llm = llm | parser
|
| 121 |
+
return chain_llm
|
| 122 |
+
|
| 123 |
+
|
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from ailab_crs import NLP_tasks_crs, Prompt_engineering_crs
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from googletrans import LANGUAGES
|
| 4 |
+
|
| 5 |
+
supported_langs = list(LANGUAGES.values())
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def main():
|
| 10 |
+
nlp_tasks = NLP_tasks_crs()
|
| 11 |
+
|
| 12 |
+
with gr.Blocks() as demo:
|
| 13 |
+
gr.Markdown("# 🧠NaanhAI💡")
|
| 14 |
+
|
| 15 |
+
with gr.Row():
|
| 16 |
+
with gr.Column():
|
| 17 |
+
text = gr.Textbox(label="Your Query", lines=8)
|
| 18 |
+
with gr.Column():
|
| 19 |
+
with gr.Accordion("Other Parameters For Translation or Summarization Tasks", open= True):
|
| 20 |
+
language = gr.Dropdown(choices=supported_langs, label="Select Target Language", value="english")
|
| 21 |
+
style = gr.Textbox(label="Choose Your Style", value = "polite")
|
| 22 |
+
# style = gr.Dropdown(choices= ["polite", "sad", "happy", "scientific", "religious"], value= "polite")
|
| 23 |
+
|
| 24 |
+
with gr.Row(scale=5):
|
| 25 |
+
with gr.Column(scale=1, min_width=1):
|
| 26 |
+
btn = gr.Button("Q&A")
|
| 27 |
+
with gr.Column(scale=2, min_width=1):
|
| 28 |
+
btn1 = gr.Button("Translator")
|
| 29 |
+
with gr.Column(scale=2, min_width=1):
|
| 30 |
+
btn2 = gr.Button("Summarizer")
|
| 31 |
+
with gr.Column(scale=2, min_width=1):
|
| 32 |
+
btn3 = gr.Button("Translator_Summarizer")
|
| 33 |
+
|
| 34 |
+
answer = gr.Textbox(label="AI Answer", lines=2)
|
| 35 |
+
|
| 36 |
+
btn.click(
|
| 37 |
+
fn= nlp_tasks.question_answer,
|
| 38 |
+
inputs= text,
|
| 39 |
+
outputs=answer
|
| 40 |
+
)
|
| 41 |
+
btn1.click(
|
| 42 |
+
fn= nlp_tasks.translator,
|
| 43 |
+
inputs= [text, language, style],
|
| 44 |
+
outputs=answer
|
| 45 |
+
)
|
| 46 |
+
btn2.click(
|
| 47 |
+
fn= nlp_tasks.summarization,
|
| 48 |
+
inputs= text,
|
| 49 |
+
outputs=answer
|
| 50 |
+
)
|
| 51 |
+
btn3.click(
|
| 52 |
+
fn= nlp_tasks.translator_summarization,
|
| 53 |
+
inputs= [text, language, style],
|
| 54 |
+
outputs=answer
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
demo.launch(share=True)
|
| 58 |
+
# demo.launch(mcp_server=True)
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
if __name__ == "__main__":
|
| 62 |
+
main()
|
pyproject.toml
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[project]
|
| 2 |
+
name = "NaanhAI"
|
| 3 |
+
version = "0.1.0"
|
| 4 |
+
description = "Add your description here"
|
| 5 |
+
readme = "README.md"
|
| 6 |
+
requires-python = ">=3.10"
|
| 7 |
+
dependencies = [
|
| 8 |
+
"googletrans>=4.0.2",
|
| 9 |
+
"gradio>=6.0.1",
|
| 10 |
+
"langchain>=1.1.0",
|
| 11 |
+
"langchain-core>=1.1.0",
|
| 12 |
+
"langchain-groq>=1.1.0",
|
| 13 |
+
"python-dotenv>=1.2.1",
|
| 14 |
+
]
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
langchain-groq
|
| 2 |
+
gradio
|
| 3 |
+
python-dotenv
|
| 4 |
+
langchain
|
| 5 |
+
langchain-core
|
| 6 |
+
googletrans
|