Spaces:
Build error
Build error
Download app.py from lindachao/GeminiStockapi: direct link, hf CLI and curl.
- Browser
- Download file 1.82 kB
-
https://huggingface.co/spaces/lindachao/GeminiStockapi/resolve/main/app.py
- Command line
-
hf download hf://spaces/lindachao/GeminiStockapi/app.py
-
curl -L -o app.py https://huggingface.co/spaces/lindachao/GeminiStockapi/resolve/main/app.py
1.82 kB
| import gradio as gr | |
| import google.generativeai as genai | |
| import httpx | |
| import tempfile | |
| import os | |
| # Retrieve API key from environment variable | |
| GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY") | |
| model = genai.GenerativeModel("gemini-1.5-flash") | |
| def invoke(question): | |
| doc_url = "https://www.twse.com.tw/pdf/ch/"+question+"_ch.pdf" | |
| doc_data = httpx.get(doc_url) | |
| if doc_data.status_code != 200: | |
| response = '查無股票代號!請輸入台灣上市股票代號!' | |
| else: | |
| with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as temp_file: | |
| temp_file.write(doc_data.content) | |
| temp_file_path = temp_file.name | |
| genai.configure(api_key=GOOGLE_API_KEY) | |
| sample_doc = genai.upload_file(temp_file_path, mime_type='application/pdf') | |
| prompt = "你是投信分析師,請以中文分項說明公司股市價量表現、融資融卷、內外資進出及財務資訊,並分析近期公司股市展望給投資人具體的專業建議!" | |
| try: | |
| response = model.generate_content([sample_doc, prompt], | |
| generation_config=genai.types.GenerationConfig( | |
| max_output_tokens=3000, | |
| temperature=0.3,) | |
| ).text | |
| except: | |
| response = 'Gemini 回應出錯!請輸入台灣上市股票代號!' | |
| return response | |
| description = """請輸入台灣上市股票代號!""" | |
| gr.close_all() | |
| demo = gr.Interface(fn = invoke, | |
| inputs = [gr.Textbox(label = "Question", lines = 1)], | |
| outputs = [gr.Textbox(label = "Response", lines = 1)], | |
| title = "Generative AI - Gemini", | |
| description = description) | |
| demo.launch() |