Update main.py
Browse files
main.py
CHANGED
|
@@ -1,10 +1,13 @@
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
from pydantic import BaseModel
|
| 3 |
import os
|
|
|
|
|
|
|
| 4 |
|
|
|
|
| 5 |
app = FastAPI()
|
| 6 |
|
| 7 |
-
# 定义请求
|
| 8 |
class RequestData(BaseModel):
|
| 9 |
content: str
|
| 10 |
|
|
@@ -12,7 +15,6 @@ class RequestData(BaseModel):
|
|
| 12 |
@app.post("/create_file")
|
| 13 |
async def create_file(data: RequestData):
|
| 14 |
try:
|
| 15 |
-
# 打开并写入文件
|
| 16 |
with open("test.txt", "w") as file:
|
| 17 |
file.write(data.content)
|
| 18 |
return {"message": "File created successfully."}
|
|
@@ -23,13 +25,44 @@ async def create_file(data: RequestData):
|
|
| 23 |
@app.get("/read_file")
|
| 24 |
async def read_file():
|
| 25 |
try:
|
| 26 |
-
# 检查文件是否存在
|
| 27 |
if not os.path.exists("test.txt"):
|
| 28 |
raise HTTPException(status_code=404, detail="File not found.")
|
| 29 |
|
| 30 |
-
# 读取文件内容
|
| 31 |
with open("test.txt", "r") as file:
|
| 32 |
content = file.read()
|
| 33 |
return {"content": content}
|
| 34 |
except Exception as e:
|
| 35 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI, HTTPException
|
| 2 |
from pydantic import BaseModel
|
| 3 |
import os
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import requests
|
| 6 |
|
| 7 |
+
# 创建FastAPI应用
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
+
# 定义POST请求的JSON格式
|
| 11 |
class RequestData(BaseModel):
|
| 12 |
content: str
|
| 13 |
|
|
|
|
| 15 |
@app.post("/create_file")
|
| 16 |
async def create_file(data: RequestData):
|
| 17 |
try:
|
|
|
|
| 18 |
with open("test.txt", "w") as file:
|
| 19 |
file.write(data.content)
|
| 20 |
return {"message": "File created successfully."}
|
|
|
|
| 25 |
@app.get("/read_file")
|
| 26 |
async def read_file():
|
| 27 |
try:
|
|
|
|
| 28 |
if not os.path.exists("test.txt"):
|
| 29 |
raise HTTPException(status_code=404, detail="File not found.")
|
| 30 |
|
|
|
|
| 31 |
with open("test.txt", "r") as file:
|
| 32 |
content = file.read()
|
| 33 |
return {"content": content}
|
| 34 |
except Exception as e:
|
| 35 |
raise HTTPException(status_code=500, detail=str(e))
|
| 36 |
+
|
| 37 |
+
# 使用Gradio创建UI界面
|
| 38 |
+
def api_request(url: str, request_type: str, json_data: str = None):
|
| 39 |
+
try:
|
| 40 |
+
if request_type == "POST":
|
| 41 |
+
# 如果是POST请求,解析JSON数据并发送
|
| 42 |
+
data = eval(json_data) if json_data else {}
|
| 43 |
+
response = requests.post(url, json=data)
|
| 44 |
+
else:
|
| 45 |
+
# 如果是GET请求,直接发送GET请求
|
| 46 |
+
response = requests.get(url)
|
| 47 |
+
|
| 48 |
+
# 返回响应内容
|
| 49 |
+
return response.json() if response.ok else f"Error: {response.status_code} - {response.text}"
|
| 50 |
+
|
| 51 |
+
except Exception as e:
|
| 52 |
+
return str(e)
|
| 53 |
+
|
| 54 |
+
# 创建Gradio界面
|
| 55 |
+
iface = gr.Interface(
|
| 56 |
+
fn=api_request,
|
| 57 |
+
inputs=[
|
| 58 |
+
gr.Textbox(label="URL", placeholder="Enter the URL to send request to"),
|
| 59 |
+
gr.Radio(["GET", "POST"], label="Request Type"),
|
| 60 |
+
gr.Textbox(label="POST JSON Data (only for POST)", placeholder="Enter JSON data, e.g. {'content': 'Hello'}", visible=False)
|
| 61 |
+
],
|
| 62 |
+
outputs="json",
|
| 63 |
+
live=True
|
| 64 |
+
)
|
| 65 |
+
|
| 66 |
+
# 启动Gradio界面
|
| 67 |
+
iface.launch()
|
| 68 |
+
|