Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| import requests | |
| import os | |
| PERPLEXITY_API_KEY = os.getenv("PERPLEXITY_API_KEY") | |
| GEMINI_API_KEY = os.getenv("GEMINI_API_KEY") | |
| def search_and_answer(question): | |
| # 1. Search using Perplexity | |
| headers = {"Authorization": f"Bearer {PERPLEXITY_API_KEY}"} | |
| data = { | |
| "query": question, | |
| "domain": ["gg.go.kr", "ggc.go.kr"] | |
| } | |
| search_resp = requests.post("https://api.perplexity.ai/search", json=data, headers=headers) | |
| search_data = search_resp.json() | |
| snippets = "\n".join([r["snippet"] for r in search_data.get("results", [])[:3]]) | |
| # 2. Generate answer with Gemini (correct model) | |
| prompt = f"์ง๋ฌธ: {question}\n\n์๋ ์๋ฃ๋ฅผ ์ฐธ๊ณ ํ์ฌ ์์ฝ๋ ๋ต๋ณ์ ์์ฑํด ์ฃผ์ธ์:\n{snippets}" | |
| gemini_url = "https://generativelanguage.googleapis.com/v1/models/gemini-1.5-pro:generateContent" | |
| headers = {"Content-Type": "application/json"} | |
| params = {"key": GEMINI_API_KEY} | |
| body = { | |
| "contents": [{"parts": [{"text": prompt}]}] | |
| } | |
| gemini_resp = requests.post(gemini_url, params=params, headers=headers, json=body) | |
| resp_json = gemini_resp.json() | |
| print("Gemini ์๋ต ์ ์ฒด:", resp_json) # ๋๋ฒ๊น ์ฉ ์ถ๋ ฅ | |
| try: | |
| return resp_json["candidates"][0]["content"]["parts"][0]["text"] | |
| except KeyError: | |
| return f"[Gemini ์ค๋ฅ] ์์๋ ์๋ต ํ์์ด ์๋๋๋ค. ์๋ต ๋ด์ฉ: {resp_json}" | |
| iface = gr.Interface(fn=search_and_answer, | |
| inputs="text", | |
| outputs="text", | |
| title="๊ฒฝ๊ธฐ๋ ์์ฐ์ด ์ง์ ์๋ต AI", | |
| description="์ง๋ฌธ์ ์ ๋ ฅํ๋ฉด ๊ฒฝ๊ธฐ๋ ๊ณต์ ์ฌ์ดํธ๋ฅผ ๊ฒ์ํ๊ณ , Gemini AI๊ฐ ์์ฝ ๋ต๋ณ์ ๋๋ฆฝ๋๋ค.") | |
| iface.launch() | |