login / app.py
fudii0921's picture
Update app.py
312a3be verified
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()