Spaces:
Sleeping
Sleeping
| # import gradio as gr | |
| # from huggingface_hub import InferenceClient | |
| # """ | |
| # For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference | |
| # """ | |
| # client = InferenceClient("HuggingFaceH4/zephyr-7b-beta") | |
| # def respond( | |
| # message, | |
| # history: list[tuple[str, str]], | |
| # system_message, | |
| # max_tokens, | |
| # temperature, | |
| # top_p, | |
| # ): | |
| # messages = [{"role": "system", "content": system_message}] | |
| # for val in history: | |
| # if val[0]: | |
| # messages.append({"role": "user", "content": val[0]}) | |
| # if val[1]: | |
| # messages.append({"role": "assistant", "content": val[1]}) | |
| # messages.append({"role": "user", "content": message}) | |
| # response = "" | |
| # for message in client.chat_completion( | |
| # messages, | |
| # max_tokens=max_tokens, | |
| # stream=True, | |
| # temperature=temperature, | |
| # top_p=top_p, | |
| # ): | |
| # token = message.choices[0].delta.content | |
| # response += token | |
| # yield response | |
| # """ | |
| # For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface | |
| # """ | |
| # demo = gr.ChatInterface( | |
| # respond, | |
| # additional_inputs=[ | |
| # gr.Textbox(value="You are a friendly Chatbot.", label="System message"), | |
| # gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"), | |
| # gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"), | |
| # gr.Slider( | |
| # minimum=0.1, | |
| # maximum=1.0, | |
| # value=0.95, | |
| # step=0.05, | |
| # label="Top-p (nucleus sampling)", | |
| # ), | |
| # ], | |
| # ) | |
| # if __name__ == "__main__": | |
| # demo.launch() | |
| import gradio as gr | |
| import requests | |
| import re | |
| # endpoint : | |
| deployment_name = "fimtrus-gpt-4o-ncus" | |
| endpoint = "https://fimtrus-openai-ncus.openai.azure.com/openai/deployments/{}/chat/completions".format(deployment_name) | |
| api_key = "FP7k76c0BGmz4MVot1LVqdtqcmQaKBeerYdFyXzW4NXN0940rRfEJQQJ99BCACHrzpqXJ3w3AAABACOG8189" | |
| ai_search_endpoint = "https://fimtrus-ai-search3.search.windows.net" | |
| ai_search_key = "lw8TYxNHT14hpM5oJQhCL3iOxR4PR494dV4RLDaiwzAzSeBecA5i" | |
| ai_search_index_name = "travel-index-microsoft2" | |
| ai_search_semantic = "travel-semantic" | |
| def request_gpt(prompt): | |
| query_params = { | |
| "api-version": "2025-02-01-preview" | |
| } | |
| headers = { | |
| "Content-Type": "application/json", | |
| "api-key": api_key | |
| } | |
| body = { | |
| "data_sources": [ | |
| { | |
| "type": "azure_search", | |
| "parameters": { | |
| "endpoint": ai_search_endpoint, | |
| "index_name": ai_search_index_name, | |
| "semantic_configuration": ai_search_semantic, | |
| "query_type": "semantic", | |
| "fields_mapping": {}, | |
| "in_scope": True, | |
| "filter": None, | |
| "strictness": 3, | |
| "top_n_documents": 3, | |
| "authentication": { | |
| "type": "api_key", | |
| "key": ai_search_key | |
| }, | |
| "key": ai_search_key | |
| } | |
| } | |
| ], | |
| "messages": [ | |
| { | |
| "role": "system", | |
| "content": "์ฌ์ฉ์๊ฐ ์ ๋ณด๋ฅผ ์ฐพ๋ ๋ฐ ๋์์ด ๋๋ AI ๋์ฐ๋ฏธ์ ๋๋ค." | |
| }, | |
| { | |
| "role": "user", | |
| "content": prompt | |
| } | |
| ], | |
| "temperature": 0.7, | |
| "top_p": 0.95, | |
| "max_tokens": 800, | |
| "stop": None, | |
| "stream": False, | |
| "frequency_penalty": 0, | |
| "presence_penalty": 0 | |
| } | |
| response = requests.post(endpoint, params=query_params, headers=headers, json=body) | |
| if response.status_code == 200: | |
| response_json = response.json() | |
| message = response_json['choices'][0]['message'] | |
| content = message['content'] | |
| content = re.sub(r'\[doc(\d+)\]', r'[์ฐธ์กฐ \1]', content) | |
| if message['context']: | |
| citation_list = message['context']['citations'] | |
| else: | |
| citation_list = list() | |
| return content, citation_list | |
| else: | |
| return "", list() | |
| with gr.Blocks() as demo: | |
| citation_state = gr.State([]) | |
| def click_send(prompt, histories): | |
| print(prompt) | |
| content, citation_list = request_gpt(prompt=prompt) | |
| message_list = [{"role": "user", "content": prompt}, {"role": "assistant", "content": content}] | |
| histories.extend(message_list) | |
| print(histories) | |
| print(len(citation_list), citation_list) | |
| return histories, citation_list, "" | |
| def select_tab(citation): | |
| print("tab!!!!!!") | |
| return gr.Textbox(label="", value=citation, lines=10, max_lines=10, visible=True) | |
| gr.Markdown("# ๋์ GPT ์๋") | |
| with gr.Row(): | |
| chatbot = gr.Chatbot(label="๋์ GPT", type="messages", scale=3) | |
| with gr.Column(scale=1): | |
| def render_citations(citations): | |
| print(citations) | |
| for index in range(len(citations)): | |
| content = citations[index]['content'] | |
| key = "์ฐธ์กฐ{}".format(index + 1) | |
| if index == 0: | |
| gr.Tab("", id=-1, visible=False) | |
| with gr.Tab(key) as tab: | |
| citation_textbox = gr.Textbox(label="", value=content, lines=10, max_lines=10, visible=False) | |
| tab.select(fn=select_tab, inputs=[citation_textbox], outputs=[citation_textbox]) | |
| with gr.Row(): | |
| prompt_textbox = gr.Textbox(label="ํ๋กฌํํธ", placeholder="ํ๋กฌํํธ๋ฅผ ์ ๋ ฅํ์ธ์.", scale=6) | |
| send_button = gr.Button("์ ์ก", scale=1) | |
| send_button.click(click_send, inputs=[prompt_textbox, chatbot], outputs=[chatbot, citation_state, prompt_textbox]) | |
| prompt_textbox.submit(click_send, inputs=[prompt_textbox, chatbot], outputs=[chatbot, citation_state, prompt_textbox]) | |
| if __name__ == "__main__": | |
| demo.launch() | |