Spaces:
Sleeping

RFC_ANY / app.py
tkpoppy's picture
Update app.py
91ae1a5 verified
Raw History Blame Contribute Delete
2.89 kB
import gradio as gr
from google import genai
import re
import os
from google.genai import types
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
client = genai.Client(api_key=GOOGLE_API_KEY)
MODEL_ID = "gemini-2.0-flash"
prompt_for_summarization = "Find the RFC number for the following protocol:"
# 提取 RFC 号码的函数
def extract_rfc_number(text: str) -> str:
match = re.search(r"RFC\s*(\d+)", text, re.IGNORECASE)
return match.group(1) if match else ""
# 处理 RFC 查询
def interact_summarization(protocol_name: str, temp=0.1) -> list:
"""
查找协议的 RFC 编号。
Arguments:
- protocol_name: 要查找 RFC 编号的协议名称。
- temp: 温度参数(控制创造性)
"""
input_text = f"{prompt_for_summarization}\n{protocol_name}"
try:
response = client.models.generate_content(
model=MODEL_ID,
contents=[{"role": "user", "parts": [{"text": input_text}]}],
config=types.GenerateContentConfig(temperature=0.1)
)
# 提取返回的文本内容
response_text = response.candidates[0].content.parts[0].text if response.candidates else "No response."
# 提取 RFC 编号
rfc_number = extract_rfc_number(response_text)
if rfc_number:
return [(f"🔍 : **{protocol_name}**", f"📄 RFC 编号: **{rfc_number}**")]
else:
return [(f"🔍 : **{protocol_name}**", "❌ 未找到对应的 RFC 号码。")]
except Exception as e:
return [(f"🔍 : **{protocol_name}**", f"⚠️ 错误: {str(e)}")]
# 重置对话
def reset() -> list:
return []
# 🎨 Gradio 美化界面
with gr.Blocks(css="""
body {
background-color: #f4f4f9;
}
.gradio-container {
max-width: 600px;
margin: auto;
padding: 20px;
background: white;
box-shadow: 0px 4px 10px rgba(0, 0, 0, 0.1);
border-radius: 10px;
}
.gr-button {
background-color: #007bff !important;
color: white !important;
border-radius: 8px !important;
font-size: 16px !important;
}
.gr-button:hover {
background-color: #0056b3 !important;
}
""") as demo:
gr.Markdown("""
# 🌍 **RFC 编号查询助手**
请输入协议名称,AI 将帮您查找对应的 **RFC 编号** 📄
""")
chatbot = gr.Chatbot()
with gr.Row():
protocol_textbox = gr.Textbox(label="🔍 输入协议名称", interactive=True, placeholder="例如:HTTP, TCP, DNS")
with gr.Row():
sent_button = gr.Button(value="📩 查询")
reset_button = gr.Button(value="🔄 清空对话")
sent_button.click(interact_summarization, inputs=[protocol_textbox], outputs=[chatbot])
reset_button.click(reset, outputs=[chatbot])
if __name__ == "__main__":
demo.launch()