当前位置: 首页 > news >正文

网站服务器多少钱一月亿速云

网站服务器多少钱一月,亿速云,汽车展示网站,wordpress directorypressGradio 是用最快的方式为你的机器学习模型制作一个友好的网页界面,让任何人都能在任何地方使用它!最近看了一两个例子。Gradio 的实现非常简单粗暴,但是界面还是非常不错。我们可以使用它快速地构建我们想要的测试界面。 在进行下面的代码之前…

Gradio 是用最快的方式为你的机器学习模型制作一个友好的网页界面,让任何人都能在任何地方使用它!最近看了一两个例子。Gradio 的实现非常简单粗暴,但是界面还是非常不错。我们可以使用它快速地构建我们想要的测试界面。

在进行下面的代码之前,建议大家先阅读我之前的文章 “Elasticsearch:在 Elastic 中玩转 DeepSeek R1 来实现 RAG 应用”。在那篇文章中,我们详细地描述了如何使用 DeepSeek R1 来帮我们实现 RAG 应用。在今天的展示中,我使用 Elastic Stack 9.1.2 来展示。

alice_gradio.py 

## Install the required packages
## pip install -qU elasticsearch openai
import os
from dotenv import load_dotenv
from elasticsearch import Elasticsearch
from openai import OpenAI
import gradio as gr
import subprocessload_dotenv()ELASTICSEARCH_URL = os.getenv('ELASTICSEARCH_URL')
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
ES_API_KEY = os.getenv("ES_API_KEY")
DEEPSEEK_URL = os.getenv("DEEPSEEK_URL")es_client = Elasticsearch(ELASTICSEARCH_URL,ca_certs="./http_ca.crt",api_key=ES_API_KEY,verify_certs = True
)# resp = es_client.info()
# print(resp)try:openai_client = OpenAI(api_key=OPENAI_API_KEY,base_url=DEEPSEEK_URL)
except:print("Something is wrong")index_source_fields = {"book_alice": ["content"]
}def get_elasticsearch_results(query):es_query = {"retriever": {"standard": {"query": {"semantic": {"field": "content","query": query}}}},"highlight": {"fields": {"content": {"type": "semantic","number_of_fragments": 2,"order": "score"}}},"size": 3}result = es_client.search(index="book_alice", body=es_query)return result["hits"]["hits"]def create_openai_prompt(results):context = ""for hit in results:## For semantic_text matches, we need to extract the text from the highlighted fieldif "highlight" in hit:highlighted_texts = []for values in hit["highlight"].values():highlighted_texts.extend(values)context += "\n --- \n".join(highlighted_texts)else:context_fields = index_source_fields.get(hit["_index"])for source_field in context_fields:hit_context = hit["_source"][source_field]if hit_context:context += f"{source_field}: {hit_context}\n"prompt = f"""Instructions:- You are an assistant for question-answering tasks using relevant text passages from the book Alice in wonderland- Answer questions truthfully and factually using only the context presented.- If you don't know the answer, just say that you don't know, don't make up an answer.- You must always cite the document where the answer was extracted using inline academic citation style [], using the position.- Use markdown format for code examples.- You are correct, factual, precise, and reliable.Context:{context}"""return promptdef generate_openai_completion(user_prompt, question, official):response = openai_client.chat.completions.create(model='deepseek-chat',messages=[{"role": "system", "content": user_prompt},{"role": "user", "content": question},],stream=False)return response.choices[0].message.contentdef rag_interface(query):elasticsearch_results = get_elasticsearch_results(query)context_prompt = create_openai_prompt(elasticsearch_results)answer = generate_openai_completion(context_prompt, query, official=True)return answerdemo = gr.Interface(fn=rag_interface,inputs=gr.Textbox(label="输入你的问题"),# outputs=gr.Markdown(label="RAG Answer"),outputs=gr.Textbox(label="RAG Answer"),title="Alice in Wonderland RAG QA",description="Ask a question about Alice in Wonderland and get an answer based on retrieved passages."
)demo.launch()# if __name__ == "__main__":
#     # question = "Who was at the tea party?"
#     question = "哪些人在茶会?"
#     print("Question is: ", question, "\n")#     elasticsearch_results = get_elasticsearch_results(question)
#     context_prompt = create_openai_prompt(elasticsearch_results)#     openai_completion = generate_openai_completion(context_prompt, question, official=True)
#     print(openai_completion)

这里的代码是从 Playground 里下载而来。我做了一下改动。为了能够使得我们每次都能输入我们想要的查询而不用重新运行代码,我添加了如下的代码:

def rag_interface(query):elasticsearch_results = get_elasticsearch_results(query)context_prompt = create_openai_prompt(elasticsearch_results)answer = generate_openai_completion(context_prompt, query, official=True)return answerdemo = gr.Interface(fn=rag_interface,inputs=gr.Textbox(label="输入你的问题"),# outputs=gr.Markdown(label="RAG Answer"),outputs=gr.Textbox(label="RAG Answer"),title="Alice in Wonderland RAG QA",description="Ask a question about Alice in Wonderland and get an answer based on retrieved passages."
)

就是这几行代码。它能帮我构建我们想要的界面。我们运行上面的代码:

python alice_gradio.py 
$ python alice_gradio.py 
* Running on local URL:  http://127.0.0.1:7860
* To create a public link, set `share=True` in `launch()`.

如上所示,我们打开页面 http://127.0.0.1:7860

哪些人在茶会上?

我们也可以用英文进行提问:

who were at the tea party?

http://www.dtcms.com/a/466763.html

相关文章:

  • MySQL数据库远程无法连接
  • 做网站实训报告电子商务网站建设的四个步骤
  • 外贸门户网站seo系统源码出售
  • 6.java反射
  • 怎么做淘宝客个人网站网站程序模板
  • 即梦图片批量去水印软件运营大管家AI图片去水印工具
  • 做网站怎么套模板网站站建设建技设术技术
  • Vue 程序使用host 0.0.0.0 实现监听本机所有可用的网络接口
  • ts-jest与其他TypeScript测试工具的对比
  • 学习16天:pytest学习
  • 奉贤青岛网站建设广州市制网公司
  • 江西中恒建设集团网站网站字体怎么设置
  • 泰安网站制作哪家好网站建设目的分析
  • 怎么看网站开发者页面渗透wordpress论坛
  • 求个网站好人有好报百度贴吧商城类网站建设方案
  • 使用 python-docx 库操作 word 文档(3):读取word文档的内容
  • 鸿蒙:从相册中选取图片,并转成PixelMap作为UI显示
  • 什么是网站什么是网址东莞网站设计服务商
  • STM32 读取引脚状态 -按键输入
  • 关于网站建设分类名字logo设计在线生成免费
  • 网站的二级网页关键词在线一键生成网页
  • 深圳夫博网站建设有限公司深夜的fm免费看
  • MySQL学习笔记09:MySQL高级特性深度学习(上):count函数、数据类型与分库分表核心原理
  • 失智老年人照护实训室建设指南:从意义到实践的全面解析
  • 10分钟上手OpenAI Agents SDK
  • 中文分析原始
  • JP4-8-MyLesson前台前端(二)
  • 做js题目的网站上海网站建设公司推荐排名
  • 网站建设效益分析代运营合同模板
  • 京东商品关键字搜索接口深度开发:从精准检索到商业机会挖掘的技术实现