| import gradio as gr |
| from groq import Groq |
| from huggingface_hub import list_models |
| import os |
| from dotenv import load_dotenv |
| import requests |
| import json |
|
|
| load_dotenv(verbose=True) |
| sqlcmd = os.environ.get("KEYURL") |
|
|
| lresponse = requests.get(sqlcmd) |
| loginfo= lresponse.json() |
|
|
| groqkey = next((item['key'] for item in loginfo if item['api'] == 'GROQ_API_KEY'), None) |
|
|
| def toggle_all(prompt): |
| |
| if not prompt == '': |
| client = Groq(api_key=groqkey) |
| |
| response = client.chat.completions.create( |
| messages=[ |
| { |
| "role": "system", |
| "content": "you are a helpful assistant." |
| }, |
| { |
| "role": "user", |
| "content": prompt, |
| } |
| ], |
| model="llama-3.3-70b-versatile", |
| ) |
| answer = response.choices[0].message.content |
| |
| return gr.update(visible=True), answer |
| else: |
| print("no prompt") |
|
|
| def hello(profile: gr.OAuthProfile | None) -> str: |
| |
| |
| if profile is None: |
| return "⛔️" |
| return f"ようこそ! {profile.name}さん" |
|
|
|
|
| def login_model(title: str) -> tuple[str, gr.update, gr.update, gr.update]: |
| |
| |
| return title, gr.update(visible=True), gr.update(visible=True), gr.update(visible=True) |
|
|
|
|
| |
| def list_private_models(profile: gr.OAuthProfile | None, oauth_token: gr.OAuthToken | None) -> tuple[str, gr.update, gr.update]: |
| |
| |
| gr.Textbox(oauth_token) |
| if oauth_token is None: |
| return "Please log in to list private models.", gr.update(visible=False), gr.update(visible=False), gr.update(visible=False) |
| |
| |
| |
| |
| |
| return profile.username, gr.update(visible=True), gr.update(visible=True), gr.update(visible=True) |
|
|
|
|
| def process_eprag(prompt): |
| if prompt == "": |
| return "プロンプトを入力してください。", "プロンプトは必須です。" |
| else: |
| url = 'http://www.ryhintl.com/eprag-be/llm?query='+prompt |
| res = requests.get(url) |
| rtn = res.content.decode('utf-8') |
| return rtn |
|
|
|
|
| def process_agent(prompt): |
| if prompt == "": |
| return "プロンプトを入力してください。", "プロンプトは必須です。" |
| else: |
| url = 'https://www.ryhintl.com/crewai/autogen?qry='+prompt |
| res = requests.get(url) |
| rtn = res.content.decode('utf-8') |
| |
| parsed_data = json.loads(rtn) |
| content_list = [entry['content'] for entry in parsed_data['chat_history']] |
|
|
| for content in content_list: |
| mycontent = content |
| |
| return mycontent |
|
|
|
|
| with gr.Blocks() as agentic: |
| gr.Markdown( |
| "# Gradio OAuth Space for Agentic RAG" |
| |
| |
| |
| |
| |
| ) |
| |
| |
| m1 = gr.Markdown() |
| m2 = gr.Markdown() |
| agentic.load(hello, inputs=None, outputs=m1) |
|
|
| with gr.Tab("LLM", visible=False) as tab_llm: |
| with gr.Column(): |
| prompt = gr.Textbox(visible=True, label="プロンプト") |
| resp = gr.Textbox(visible=True, label="レスポンス") |
| show_button = gr.Button("生成") |
| |
| show_button.click(fn=toggle_all, inputs=prompt, outputs=[tab_llm, resp]) |
|
|
| with gr.Tab("EPRAG", visible=False) as tab_eprag: |
| |
| |
| gr.Markdown("# 🗞️ AGENTIC EPRAG") |
| |
| with gr.Row(): |
| eprag_input = gr.Textbox(label="プロンプト", type="text") |
| |
| with gr.Row(): |
| eprag_output = gr.Textbox(label="AIアシスタントの応答") |
| |
| submit_button = gr.Button("EPRAGプロセス", variant="primary") |
| submit_button.click( |
| process_eprag, |
| inputs=[eprag_input], |
| outputs=[eprag_output] |
| ) |
|
|
| with gr.Tab("AGENT", visible=False) as tab_agentic: |
| |
| |
| gr.Markdown("# 🗞️ AGENTIC AUTOGEN") |
| |
| with gr.Row(): |
| agent_input = gr.Textbox(label="プロンプト", type="text") |
| |
| with gr.Row(): |
| agent_output = gr.Textbox(label="Agentアシスタントの応答") |
| |
| submit_button = gr.Button("AGENTICプロセス", variant="primary") |
| submit_button.click( |
| process_agent, |
| inputs=[agent_input], |
| outputs=[agent_output] |
| ) |
|
|
| |
| agentic.load(login_model, inputs=None, outputs=[m2, tab_llm, tab_eprag, tab_agentic]) |
| |
|
|
| agentic.launch() |