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("SQLCMD_URL") 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 #answer = prompt+" answered." return gr.update(visible=True), answer else: print("no prompt") def hello(profile: gr.OAuthProfile | None) -> str: # ^ expect a gr.OAuthProfile object as input to get the user's profile # if the user is not logged in, profile will be None if profile is None: return "⛔️" return f"ようこそ! {profile.name}さん" #def list_private_models(profile: gr.OAuthProfile | None, oauth_token: gr.OAuthToken | None) -> str: def list_private_models(profile: gr.OAuthProfile | None, oauth_token: gr.OAuthToken | None): # ^ expect a gr.OAuthToken object as input to get the user's token # if the user is not logged in, oauth_token will be None 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) #models = [ #f"{model.id} ({'private' if model.private else 'public'})" #for model in list_models(author=profile.username, token=oauth_token.token) #] #return "Models:\n\n" + "\n - ".join(models) + ".", gr.update(visible=True) 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" #"\n\nThis Space is a agentic demo for the **Sign in with Hugging Face** feature. " #"Duplicate this Space to get started." #"\n\nFor more details, check out:" #"\n- https://www.gradio.app/guides/sharing-your-app#o-auth-login-via-hugging-face" #"\n- https://huggingface.co/docs/hub/spaces-oauth" ) gr.LoginButton() # ^ add a login button to the Space 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(list_private_models, inputs=None, outputs=[m2, tab_llm, tab_eprag, tab_agentic]) agentic.launch()